0

I would like to extract specific values from an unformatted text file to create a new file with each value on a single row. For example in this file...

Calculating outliers
Range of metric values: 0.016416 1.319020 
Found 8 outliers over 0.3
Generating EVs
Found spikes at  77 132 140 142 155 156 157 175
Script complete

I would like to extract all values between "Found spikes at " and "Script complete" to create a file that would look like this

77
132 
140 
142 
155 
156 
157 
175

I should also note that there will not always be the same number of values extracted. There could be anywhere from 0 to 20+ values.

3
$ sed -n '/^Found spikes at  */s///p' file | tr ' ' '\n'
77
132
140
142
155
156
157
175

This uses sed to locate the line that starts with the string Found spikes at followed by some number of spaces. When the line is found, that string, including the spaces before the numbers, is removed from it and the remaining bit is printed to standard output. The standard output of sed is read by tr which replaces any space with a newline.

Use a redirection at the end to save the result in a new file:

$ sed -n '/^Found spikes at  */s///p' file | tr ' ' '\n' >newfile

Modified after comment by ctac_ to remove the need for that extra tr step:

sed -n '/^Found spikes at  */s///p; y/ /\n/' file >newfile
  • Thanks so much! How would I output this to it's own unique text file? – Rachel Jul 30 '18 at 16:22
  • @Rachel See updated answer. – Kusalananda Jul 30 '18 at 16:22
  • 1
    perhaps sed '/^Found spikes at */!d;s///;y/ /\n/' infile without tr – ctac_ Jul 30 '18 at 18:14
  • @ctac_ Yes, that would work too. – Kusalananda Jul 30 '18 at 18:36

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.