0

I am tring to extract text from a multi-line file. For example I need to extract all text from "Section 1.0" to "Section 3.0"

This can be on many lines.

I have code that works, but seems clumsy and slow. Is there a better way to do this? sed? reg expression?

flag="false"

for line in ${textFile}; 
do
   if [ "$line" == "Section 3.0" ]; then
      flag="false"
   fi
   if [ "$flag" == "true" ]; then
      temp_var+=$line
   fi
   if [ "$line" == "Section 1.0" ]; then
      flag="true"
   fi
done

3 Answers 3

3

Using sed you can do:

sed -n '/Section 1\.0/,/Section 3\.0/p' file

EDIT: To ignore start and end patterns use:

sed -n '/Section 1\.0/,/Section 3\.0/{/Section [13]\.0/!p;}' file

awk solution:

awk '/Section 1\.0/{flag=0} flag{print} /Section 3\.0/{flag=1}' file
Sign up to request clarification or add additional context in comments.

1 Comment

Yes section 1 comes before section 3. The code is the way it is so the actual section title does not get written.
2
sed -n '/Section 1\.0/,/Section 3\.0/p' file

will print from file all lines between a line matching the first regex anywhere in it through the next line matching the second expression. If there are multiple such matches, they will be printed in flip-flop fashion (look for pattern 1, print through pattern 2, look for pattern 1...)

If you want only the first such section, you can quit when you find the end condition:

sed -n '/Section 3\.0/q;/Section 1\.0/,$p' file

This will exclude the line matching the end condition (guessing that's what you actually want). For simplicity, this assumes you have no Section 3.0 before Section 1.0. (Some sed dialects might require slighly different syntax; the semicolon may have to be changed to a newline, or the script split into two separate -e arguments.)

Comments

0

awk can also be used:

awk '/Section 3\.0/{f=0} f; /Section 1\.0/{f=1}' file

Comments

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.