I have to develope unix script in such way it should meet below scenario.

I have 3 text files, 1.txt , 2.txt and 3.txt

Data in those files as below.


For the first file (1.txt), we have to remove line based on TLR tag. For the second file (2.txt), we have to remove lines based on both TLR and HDR. For the 3rd file (3.txt), we have to remove line based on HDR tag.

After doing above operation I have to put those three files data into single file.

Here we are not sure how many files we get to merge it.we cannot directly hard code file names (like above 3 text files).

How can we achieve this script?

  • take a look at grep -v and then redirect the output to a destination file. Use ">>" for incremental append.
    – BitsOfNix
    Aug 13, 2013 at 9:13

1 Answer 1


This can simply be done by using a answer from your last, related question and some output redirecting:

grep -vE "TLR" 1.txt > merged.txt
grep -vE "TLR|HDR" 2.txt >> merged.txt
grep -vE "HDR" 3.txt >> merged.txt

You can also group those commands together using a sub-shell:

( grep -vE "TLR" 1.txt ;  grep -vE "TLR|HDR" 2.txt ; grep -vE "HDR" 3.txt  )> merged.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .