awk - Grep specific part of string from another file -
i want grep first 3 digits of numbers in 1.txt first 3 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
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 first 3 characters in associative array.
match($1, /0+/);if(substr($1, rstart+rlength,3) in a)
matches 3 charaacters after series of zeroes , checks whether these 3 characters present in associative array created earlier , prints whole line if match found.
Comments
Post a Comment