user934718
user934718

Reputation: 55

Match text only from first line and last line?

How do I search text in the first line with regex and text in the last line with another regex (multi-line string)

  (blablabla)
  blablabla
  blablabla
  (blablabla)
  blablabla

If there are two lines with text in parentheses, one regex to match only text from first line that contains parentheses and one regex to match only text from second line that contains parentheses.

Upvotes: 1

Views: 1145

Answers (1)

Chris
Chris

Reputation: 17992

Since you made the question more clear, here you go:

egrep "\(.*\)" <inputfile> | tail -1

The egrep finds lines which contain text surrounded by parentheses. tail -1 trims the output to the last line found.

Since I'm pretty sure you're not searching for things that look like (aabssa), http://rubular.com/ is an excellent regex reference/tester.

Old answer below

Since it's unclear you need to do these in the same step, or even compare them, how about this for the Unix shell.

First Line:head -n 1 <input file> | egrep "<regex>"

Last Line:tail -n 1 <input file> | egrep "<regex>"

Give us more info, and you'll probably get a more helpful answer.

Upvotes: 2

Related Questions