starpark
starpark

Reputation: 195

Grep specific part of string from another file

I want to grep the first three digits of numbers in 1.txt from the first three digits after zeros in 2.txt.

cat 1.txt

 23456
 12345
 6789

cat 2.txt

 20000023485 xxx888
 20000012356 xxx888
 20000067234 xxx234

Expected output

 20000023485 xxx888
 20000012356 xxx888

Upvotes: 0

Views: 121

Answers (3)

Vijay
Vijay

Reputation: 67319

awk 'FNR==NR {a[substr($1,0,3)];next}
             {match($1, /0+/);
             if(substr($1, RSTART+RLENGTH,3) in a)print}' 1.txt 2.txt

{a[substr($1,0,3)];next} - stores the first 3 characters in an associative array.

match($1, /0+/);if(substr($1, RSTART+RLENGTH,3) in a) Matches the 3 charaacters after the series of zeroes and checks whether these 3 characters are present in the associative array that was created earlier and prints the whole line if match is found.

Upvotes: 1

Cyrus
Cyrus

Reputation: 88999

Try this with grep:

grep -f <(sed 's/^\(...\).*/00\1/' file1) file2

Output:

20000023485 xxx
20000012356 xxx

Upvotes: 1

Derek T. Jones
Derek T. Jones

Reputation: 1822

grep -f will match a series of patterns from the given file, one per line. But first you need to turn 1.txt into the patterns you want. In your case, you want the first three characters of each line of 1.txt, after zeros: 00*234, 00*123, etc. (I'm assuming you want at least one zero.)

sed -e 's/^\(...\).*$/00*\1/' 1.txt > 1f.txt
grep -f 1f.txt 2.txt

Upvotes: 0

Related Questions