bash - How could I remove a row which three components of a row in one file is similar to another file? -
this question has answer here:
i have 2 files format of data , space character in them different. want compare these 2 files , if first 3 columns of each row in file1 similar file2, remove these rows file2 , save rest of file2 in file2.
example:
file1: 4775.00 5215.28 23.9655 7 2.54766 4568.06 5611.74 22.0619 56 2.36560 file2: 6944.69 5605.22 22.6416 44 2.31929 0.914626 1619.15 5597.93 23.7979 12 2.38169 0.684619 5986.66 5599.89 23.8822 6 2.99171 0.384949 4568.06 5611.74 22.0619 56 2.36560 0.973764 1365.00 5601.16 23.3325 15 3.78138 0.212834 1787.81 5209.01 23.4643 8 3.34705 0.486087 5287.90 5244.40 21.9990 104 5.28082 0.000269982 4775.00 5215.28 23.9655 7 2.54766 0.467107 9024.54 5235.34 21.0784 120 3.13061 0.0357055
cheers.
i'd forward when needed. meanwhile here's solution:
#!/usr/bin/awk -f fnr == nr { a[$1] = nr b[$2] = nr c[$3] = nr next } $1 in { t = a[$1] if (b[$2] == t && c[$3] == t) { next } } { r[++i] = $0 } end { (j = 1; j <= i; ++j) { print r[j] > argv[2] } }
warning: directly modifies file required.
usage:
awk -f script.awk file1 file2
example result:
6944.69 5605.22 22.6416 44 2.31929 0.914626 1619.15 5597.93 23.7979 12 2.38169 0.684619 5986.66 5599.89 23.8822 6 2.99171 0.384949 1365.00 5601.16 23.3325 15 3.78138 0.212834 1787.81 5209.01 23.4643 8 3.34705 0.486087 5287.90 5244.40 21.9990 104 5.28082 0.000269982 9024.54 5235.34 21.0784 120 3.13061 0.0357055
if don't want have direct modification second file, don't use redirection on last part:
end { (j = 1; j <= i; ++j) { print r[j] } }
Comments
Post a Comment