BRZ
BRZ

Reputation: 695

String Extraction from file

I have a file with the following lines:

Aug 25 11:20:19   master set to 'John Jacob' (ID 12345) based on [Created Date] = '2014-05-30 20:58:59'
Aug 25 11:20:19  some text
Aug 25 11:20:49   master set to 'Jane Joy' (ID 6789) based on [Created Date] = '2014-05-30 20:59:07'
Aug 25 11:20:19 more text
Aug 25 11:20:19  more text ("ID not found")
Aug 25 11:20:19  master is even more text ("ID Not found") but alternate ID (ID 11358) exists"

I'm trying to extract the string beginning with "master" until the ")" of the (ID 12345).

This is what I'm trying to do:

grep "master set to" | cut -d ")" -f input.txt

However when I run this, everything in the text file returns. Is my usage of grep and cut in correct?

Thanks in advance, simak

Upvotes: 0

Views: 33

Answers (4)

Jotne
Jotne

Reputation: 41446

This awk may do:

awk -F"master " 'NF>1 {split($2,a,")");print a[1]")"}' file
set to 'John Jacob' (ID 12345)
set to 'Jane Joy' (ID 6789)
is even more text ("ID Not found")

Upvotes: 0

Jay
Jay

Reputation: 2686

You can try regular expressions with grep

grep 'master.+\)' inputfile.txt > outputfile.txt

Upvotes: 0

damienfrancois
damienfrancois

Reputation: 59070

your line should really be

grep "master set to" input.txt | cut -d ")" -f 1

Note however that it will strip the trailing ) and will keep the date at the beginning.

$ grep "master set to" input.txt | cut -d")" -f1
Aug 25 11:20:19   master set to 'John Jacob' (ID 12345
Aug 25 11:20:49   master set to 'Jane Joy' (ID 6789

See @Codegnome's answer for a better approach still using grep

Upvotes: 0

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

Use GNU Grep's --only-matching Flag

$ grep --only-matching 'master.*)' input.txt 
master set to 'John Jacob' (ID 12345)
master set to 'Jane Joy' (ID 6789)
master is even more text ("ID Not found") but alternate ID (ID 11358)

Upvotes: 2

Related Questions