Print out "string" between "match1" and "match2"

echo "string" | sed -e 's/.*match1//' -e 's/match2.*$//'
But who knows to delete the rest of the lines? I want only "string".

-3
By: P17
2009-04-01 15:01:27

These Might Interest You

What Others Think

Not entirely sure what you're wanting, this maybe? sed -e 's/^.*\(string\).*$/\1/'
TheMightyBuzzard · 480 weeks and 4 days ago
This should be what you're looking for. Only prints lines with match1 and match2 in them, and only the section between the two matches. sed -ne 's/.*\(match1.*match2\).*/\1/p' or, a bit easier: egrep -o 'match1.*match2' Works with standard grep here, too, but YMMV.
Viaken · 480 weeks and 4 days ago
If you don't know what you're after, why are you posting to CLF? This isn't a support forum, but a repository of the best commands on the web. BTW: echo "string" | awk '/match1/,/match2/'
atoponce · 480 weeks and 4 days ago
I got it: sed -n '/match1/,/match2/p' file.txt | sed -e '/match1/d' | sed -e '/match2/d'
P17 · 477 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: