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.
Make sure you quote your expression.
$ grep ' \.pdf' example
grep .pdf
Or if there might be multiple spaces (we can't use *
as this will match the cases where there are no preceding spaces)
grep ' \+\.pdf' example
+
means "one or more of the preceding character". In BRE you need to escape it with \
to get this special function, but you can use ERE instead to avoid this
grep -E ' +\.pdf' example
You can also use \s
in grep
to mean a space
grep '\s\+\.pdf' example
We should escape literal .
because in regex .
means any character, unless it's in a character class.
Best Answer
Here's the answer: For instance these are the texts inside the file
To get the
+45.0°C
of temp1, use this command: