prashant
prashant

Reputation: 21

How to grep the string with specific pattern

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.

Upvotes: 2

Views: 11559

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185770

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
cp 'cp'
.* any character except \n (0 or more times (matching the most amount possible))
TC_12_IT_ 'TC_12_IT_'
\d{6} digits (0-9) (6 times)
_ _
\d{6} digits (0-9) (6 times)
TC_12_TEST 'TC_12_TEST'
\d{2} digits (0-9) (2 times)
. any character except \n
tc12 'tc12'
. any character except \n
tc12 'tc12'

Upvotes: 1

Timur Shtatland
Timur Shtatland

Reputation: 12465

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

Upvotes: 1

Related Questions