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!

  • cat file.txt | grep 'Values a | b | c |d' returns the same number of lines? – gwarah Jul 9 '16 at 17:45
  • 1
    I strongly suggest you post sample portion of your input file. Otherwise , the answers posted will be as good as guessing. I understand what you want - read all the data from file and turn it into cvs table , but we need to have some example to work with. – Sergiy Kolodyazhnyy Jul 9 '16 at 21:08

You could use paste

paste -d, {a..d}.csv > all.csv

or (skipping the intermediate files by using process substitution)

paste -d, <(grep 'Values a' file.txt | cut -d' ' -f9) <(grep 'Values b' file.txt | cut -d' ' -f9) <(grep 'Values c' file.txt | cut -d' ' -f9) <(grep 'Values d' file.txt | cut -d' ' -f9)

There are likely more efficient and elegant ways to do what you want that don't involve making multiple passes of the same file - but you would need to share an example that displays the format of your file.txt

  • That's efficient and elegant enough for me. Thanks a lot! – Joe Doe Jul 9 '16 at 17:48

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.

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.