1

I am quite new to bash scripting and so any help would be much appreciated. Below is what I want to achieve.

I have two text files. I want to delete all of the lines on the first file where it matches any of my string on the second file before the a comma.

e.g.

File 1:

this_is_a_test.txt,11dsdsdsdsd
this_is_a_test24.txt,545467ddd
this_is_a_test22,121244442

File 2:

this_is_a_test.txt
this_is_a_test24.txt
this_is_a_test22

Desired Output: Blank

  • for your given sample, a simple grep -Fvf file2 file1 will work, assuming the strings in file2 will only match first column data of file1 – Sundeep Apr 7 '17 at 13:14
0

You can use:

grep -vFf file2 file1

Beware that it still delete lines although it matches after the comma.

0

A simple sed trick can do this.

IFS='
' # If whitespaces exist in file 2
cat file2.txt | while read i; do
  sed -i '/^'"$i"',/d' file1.txt
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.