Ubuntu – How to Export several grep outputs into one file

filesgrepmergeoutput

I have a little grep command line that finds certain values in a text file and exports them into a .csv-file.

Here it is:

cat file.txt | grep 'Values a' | cut -d' ' -f9 >a.csv
cat file.txt | grep 'Values b' | cut -d' ' -f9 >b.csv
cat file.txt | grep 'Values c' | cut -d' ' -f9 >c.csv
cat file.txt | grep 'Values d' | cut -d' ' -f9 >d.csv

How can I modify the commands to get one (comma separated) .csv-file with four columns a, b, c, d?
Any help is appreciated!

Best Answer

As the first comment said, without a view of your "file.txt" it is tough to know for sure we are getting this right.

My first thought was why don't you take the easy road? The >> will append, so create the one thing, add the rest.

cat file.txt | grep 'Values a' | cut -d' ' -f9 > a.csv
cat file.txt | grep 'Values b' | cut -d' ' -f9 >> a.csv
cat file.txt | grep 'Values c' | cut -d' ' -f9 >> a.csv
cat file.txt | grep 'Values d' | cut -d' ' -f9 >> a.csv

Then I wondered why do you run cat at all

grep 'Values a' file.txt | cut -d' ' -f9 > a.csv
grep 'Values b' file.txt | cut -d' ' -f9 >> a.csv
grep 'Values c' file.txt | cut -d' ' -f9 >> a.csv
grep 'Values d' file.txt | cut -d' ' -f9 >> a.csv

That works, but it puts the 4 things on new lines, rather than as comma separated values.

So I concentrated on stopping the new lines from being inserted, and getting a comma instead. awk can handle that.

grep 'Values a' file.txt | cut -d' ' -f9 | awk '{printf $0 ","}' > a.csv
grep 'Values b' file.txt | cut -d' ' -f9 | awk '{printf $0 ","}' >> a.csv

and so forth. That works, but right before I hit "send" I realized that I was following along with you using cut and we don't need to. awk can select the 9th thing.

grep 'Values a' file.txt | awk '{printf $9 ","}' > a.csv
grep 'Values b' file.txt | awk '{printf $9 ","}' >> a.csv

That worked on a little test case I made, maybe not on your test case.

If I were doing repeated greps on data to build a file, I'd probably prefer to keep it in separate lines, rather than in a big, complicated tangle like the other one recommended. So I don't think I'd go for a one liner, four lines I can understand is good enough.

If you are going to do more shell work like this, I suggest you get a copy of the old book Unix Power Tools. It has a lot of tricks like this.