简体   繁体   中英

Bash, selecting text surrounded by specific matches

What I need to do is print out the text which starts after the particular match and ends before another match.

Let's say I have a big output and I only need to print the text which starts after line '$$$$$$$$$$' and ends before line '((((((((('. Oh and the file might end without the '(((((((((' line, in which case I would still need that text after '$$$$$$$$$$' till the end of the output.
How would I do that?

You can use sed :

sed -e '/\$\{10\}/,/(\{9\}/!d' inputfile

To exclude the boundary lines, you have to do some programming:

sed -ne '/\$\{10\}/ {n;b c}; d;:c {/(\{9\}/ {d};p;n;b c}' inputfile

You can use awk

awk '/START/{f=1;next} /END/{f=0} f' file

You can set start stop to what you like.

cat file

test
$$$$$$$$$$
more
data
(((((((((
stop

awk '/\$\$\$\$\$\$\$\$\$\$/{f=1;next} /\(\(\(\(\(\(\(\(\(/{f=0} f' t
more
data

Since $ and ( is special characters, you need to escape them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM