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.

1 Answer 1

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

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.