1

I'm trying to select certain text from an output file.

I'm reading my file as follows:

while read line 
do
    if [ "$line" == "SUMMARY OF POLARIZATION CALCULATION" ]; then
        break
    fi
done < tutorial1/Tutorial1_1.out

When the loop reaches that "Summary" line, i need to read only the next 9 lines. I'm trying to use a for loop but i'm not sure how to use it:

for i in {1..9}
do
    read line < tutorial1/Tutorial1_1.out
    echo $line >> Summary.out
done

My output is as follows:

next is setrmt
next is setrmt
next is setrmt
next is setrmt
next is setrmt
next is setrmt
next is setrmt
next is setrmt
next is setrmt

But i need it to be the next 9 lines after the "SUMMARY" statement. Please help.

2
  • Where's the file you're reading really? Is it in tutorial1/Tutorial1_1.out or in ../Tutorial1_1.out? These are different files unless tutorial1 is a symlink to the parent directory. Commented Sep 2, 2014 at 16:18
  • Sorry about that, the file is in /tutorial1 Commented Sep 2, 2014 at 16:25

3 Answers 3

3

You can use the -A parameter for the grep command like:

grep -A9 "SUMMARY OF POLARIZATION CALCULATION"

from the man:

-A NUM, --after-context=NUM

          Print NUM lines of trailing context after matching lines.

demo:

while read -r line
do
    echo "$line"
done < <(grep -A9 "SUMMARY OF POLARIZATION CALCULATION" filename | tail -9)

for the next input file

before1
before2
before3
before4
before5
before6
SUMMARY OF POLARIZATION CALCULATION
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12

prints:

line1
line2
line3
line4
line5
line6
line7
line8
line9

or simply:

 grep -A9 "SUMMARY OF POLARIZATION CALCULATION" ../Tutorial1_1.out | tail -9 >> Summary.out
Sign up to request clarification or add additional context in comments.

Comments

2

You can't redirect again. That reopens the file at the beginning. Do the second block inside the first and use the same file descriptor:

while read line 
do
    if [ "$line" == "SUMMARY OF POLARIZATION CALCULATION" ]; then
        for i in {1..9}
        do
            read line
            echo $line >> Summary.out
        done
        break
    fi
done < tutorial1/Tutorial1_1.out

1 Comment

You can also replace the entire for loop with head when sharing the file descriptor. { while read line; do if [ ... ]; then break; fi; done; head -9 > Summary.out; } < tutorial1/Tutorial1_1.out.
0

Let's try a clean solution:

1) find the line number of the line that contains the string you want. You could do this by implementing a counter, but this is better:

linenr=$(grep -n -m 1 "SUMMARY OF POLARIZATION CALCULATION" <your file name here> | cut -d':' -f1)

2) calculate the number of the last line you want:

let "lastlinenr = $linenr + 9"

3) fetch the lines and write them to a file:

cat <your file name here> | sed -n "$linenr","$lastlinenr"p > <your destination file here>

1 Comment

I tried this method, but it returns an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.