How to both extract a specific line in a text file as well as multiple lines containing a specific string

grepsedtext processing

I have a collection of text files containing more data than I need. Each file's first line contains a comma-separated string that looks like this:

stop_id,stop_code,stop_name,stop_desc,stop_lat,stop_lon,location_type,parent_station,zone_id

Then, below those keys is all the data. I need to extract a subset of that data into a new text file so I can work with the subset (I don't need all the data, it's too much).

I'm using this command to extract the first line:

sed -n '1p' source.txt > destination.txt

I'm also using this command to extract the specific lines I need:

grep "string" source.txt > destination.txt

The challenge is that when I run the two commands in the same script (pretty much as is, separated by a line or &&), the grep output overwrites the sed output. How can I run both in sequence and have the combined output of both?

I noticed a question that seems similar and involves using a more complex grep command to locate the one line, followed by a range of lines. That won't work here because the first line of each of the files I need to extract data from is different.

Ideally, I want to write a function that I can run against each of the files I need to work with but I need to chain these commands and combine their outputs first.

Best Answer

Just change the grep output to append,

grep "string" source.txt >> destination.txt

Related Question