ssb
ssb

Reputation: 209

search in shell

folks

I have a output file looks like:

Title: [name of component]
**garbage output**
**garbage output**
Test run: succuess 17 failure 2
**garbage output**
**garbage output**

and there are many components like this. I cannot change the way of the output. So I'd want to just grab the lines of title and test result.

My question is, how to write a regular expression to achieve this?

I tried:

cat output | sed -e 'm/Tests run(.*)/g'

but it always complains: unknown command `m'

Other methods except regex would also be appreciated!!!

Thanks a lot

Upvotes: 0

Views: 58

Answers (1)

shellter
shellter

Reputation: 37268

You don't need cat, try

  grep -E '^Title:|^Test run' fileName

on older systems you may need to use egrep '^Title...'.

Edit for

I want exclude Title with certain prefix from the result, like "Title: foo XXX" or "Title bar XXX".

There is certainly a regex for grep -E that would handle this, but for the first few years of cmd-line work, AND as you appear to be using this to cleanup test.log output, it is good to use the unix tool box approach, in this case, 'get something working and add a little more to it', i.e.

  grep -E '^Title:|^Test run' fileName | egrep -v '^Title: foo XXX|^Title:bar XXX'

This is the power of the unix pipeline, got too much output?, then keep adding more grep -vs to clean it up.

Note that *grep -v means exclude lines that match the following patterns.

I hope this helps

Upvotes: 3

Related Questions