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 every开发者_StackOverflow中文版thing 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
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 markerp
- Print it:a
- label an
- Read the next line/^----End Piece/{
- If it's the block end markerp
- Print itb
- Branch to the end to read the next line of input
}
- end ifp
- Print a line that's within the blockba
- Branch to label a to see if there are more lines in the block
}
- end if
#!/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>&-
精彩评论