bash - egrep -v match lines containing some same text on each line -
so have 2 files.
example of file 1 content.
/n01/mysqldata1/mysql-bin.000001 /n01/mysqldata1/mysql-bin.000002 /n01/mysqldata1/mysql-bin.000003 /n01/mysqldata1/mysql-bin.000004 /n01/mysqldata1/mysql-bin.000005 /n01/mysqldata1/mysql-bin.000006
example of file 2 content.
/n01/mysqlarch1/mysql-bin.000004 /n01/mysqlarch1/mysql-bin.000001 /n01/mysqlarch2/mysql-bin.000005
so want match based on mysql-bin.00000x , not rest of file path in each file differ between file1 , file2.
here's command i'm trying run
cat file1 | egrep -v file2
the output i'm hoping here be...
/n01/mysqldata1/mysql-bin.000002 /n01/mysqldata1/mysql-bin.000003 /n01/mysqldata1/mysql-bin.000006
any appreciated.
just compare based on /
:
$ awk -f/ 'fnr==nr {a[$nf]; next} !($nf in a)' f2 f1 /n01/mysqldata1/mysql-bin.000002 /n01/mysqldata1/mysql-bin.000003 /n01/mysqldata1/mysql-bin.000006
explanation
this reads file2 in memory , compares file1.
-f/
set field separator/
.fnr==nr {a[$nf]; next}
while reading first file (file2), store every last piece arraya[]
. since set field separator/
,mysql-bin.00000x
part.!($nf in a)
when reading second file (file1) check if last field (mysql-bin.00000x
part) in arraya[]
. if not, print line.
i'm having 1 problem i've noticed when testing. if file2 empty nothing returned @ expected every line in file1 returned. me please? – user2841861.
then problem fnr==nr
matches when reading second file. prevent this, cross check "reading a[] array" action done on first file:
awk -f/ 'fnr==nr && argv[1]==filename {a[$nf]; next} !($nf in a)' f2 f1 ^^^^^^^^^^^^^^^^^^^^
from man awk
:
argv
the command-line arguments available awk programs stored in array called argv. argc number of command-line arguments present. see section other command line arguments. unlike awk arrays, argv indexed 0 argc - 1
Comments
Post a Comment