1

I need to process large amount of data from stdout, but I need only several lines from them.

Let's say I want to append some data from line beginning with Label 3 to the file F3.csv, and some data from line beginning with Label 5 to the file F5.csv.

Example input - from STDOUT

Label 1 25 Label 2 60 Label 3 70 Label 4 95 Label 5 100 

Output

Append 70 to F3.csv and 100 to F5.csv.

I'd like to know if it's possible to do this and if it is, then how.

Do you know any way?

2 Answers 2

3

That is pretty easy to do with Awk:

awk '/Label 3/ { print $3 >>"F3.csv" } /Label 5/ { print $3>>"F5.csv" }' 

This will append the value for lines starting with Label 3 and Label 5 to the file F3.csv and F5.csv correspondingly.

This can also be written a bit more compactly:

awk '/Label 3|Label 5/ { print $3 >> "F"$2".csv"}' 

using a fact that target filename can be deduced from the label suffix

(Thanks @don_crissti !)

0
2

try this -

#sed -e '/Label\ 3/w F3.csv' -e '/Label\ 5/w F5.csv' sed.txt Label 1 25 Label 2 60 Label 3 70 Label 4 95 Label 5 100 #head F?.csv ==> F3.csv <== Label 3 70 ==> F5.csv <== Label 5 100 
1
  • Thank you for your answer. I'd just like to add, that I need to append only values, not the original label. Commented Jan 31, 2017 at 21:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.