Matt
Matt

Reputation: 2843

Shell Scripting removing text before and after strings

I need to shell script a way to get the random unknown junk text out of a text file. I am stuck on how to do this because i don't know what the junk text will say. Basically i need to remove everything before, after, and in between the pieces. I want to keep the text that is inside the pieces.

--Begin file


random unknown junk text

----Begin Piece one ---- 
random important text
----End Piece one ----

random unknown junk text

----Begin Piece two ---- 
random important text
----End Piece two ----

random unknown junk text

----Begin Piece two ---- 
random important text
----End Piece two ----

random unknown junk text


end of file

Upvotes: 2

Views: 1522

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 359905

sed -n '/^\(--Begin file\|end of file\)/{p;b}; /^----Begin Piece/{p;:a;n;/^----End Piece/{p;b};p;ba}' inputfile

Explanation:

  • /^\(--Begin file\|end of file\)/{p;b} - Print the file beginning/ending lines (matches literal text)
  • /^----Begin Piece/{ - If the line matches the block begin marker
    • p - Print it
    • :a - label a
    • n - Read the next line
    • /^----End Piece/{ - If it's the block end marker
      • p - Print it
      • b - Branch to the end to read the next line of input
    • } - end if
    • p - Print a line that's within the block
    • ba - Branch to label a to see if there are more lines in the block
  • } - end if

Upvotes: 2

vissi
vissi

Reputation: 2334

#!/bin/bash
exec 3< file.txt
fl=0
regex='----Begin Piece.+'
regexe='----End Piece.+'
while read <&3
do
    if [ $fl -eq 1 ] && [[ ! "$REPLY" =~ $regexe ]]; then
        echo "$REPLY"
    fi
    if [[ "$REPLY" =~ $regex ]]; then fl=1; fi
    if [[ "$REPLY" =~ $regexe ]]; then fl=0; fi
done
exec 3>&-

Upvotes: 0

Related Questions