Home > Back-end >  How to grep the string with specific pattern
How to grep the string with specific pattern

Time:01-07

I am trying to grep a file.txt to search 2 strings cp and (target file name) where the line in file is as below,

cp (source file name) (target file name)

the problem for me here is string '(target file name)' has specific pattern as /path/to/file/TC_12_IT_(6 digits)_(6 digits)_TC_12_TEST _(2 digits).tc12.tc12

I am using below grep command to search a line with these 2 strings,

grep -E cp.*/path/to/file/TC_12_IT_ file.txt

how can I be more specific about (target file name) in grep command to search (target file name) with all its patterns, something like below,

grep -E 'cp.*/path/to/file/TC_12_IT_*_*_TC_12_TEST_*.tc12.tc12' file.txt

can we use wildcards in grep to search string in file just like we can use wilecard like * in listing out files e.g.

ls -lrt TC_12_*_12345678.txt

please suggest if there are any other ways to achieve this.

CodePudding user response:

Like this, using GNU grep:

grep -P 'cp.*?TC_12_IT_\d{6}_\d{6}TC_12_TEST\d{2}.tc12.tc12' file

The regular expression matches as follows:

Node Explanation
.*? any character except \n (0 or more times (matching the least amount possible))
\d{6} digits (0-9) (6 times)
\d{2} digits (0-9) (2 times)

CodePudding user response:

More specifically:

grep -P '^cp\s . \s \S /TC_12_IT_\d{6}_\d{6}_TC_12_TEST _\d2[.]tc12[.]tc12$' in_file > out_file

^ : beginning of the line.
\s : 1 or more whitespace characters.
. : 1 or more any characters.
\S : 1 or more non-whitespace characters.
\d{6} : exactly 6 digits.
[.] : literal dot (.). Note that just plain . inside a regular expression means any character, unless it is inside a character class ([.]) or escaped (\.).
$ : end of the line.

SEE ALSO:
GNU grep manual
perlre - Perl regular expressions

  • Related