Kalin Borisov
Kalin Borisov

Reputation: 1120

Grep specific string in mixed file on linux

How to extract only the string q9I9YP1V013809 from this output:

q9I9YP1V013809     1472 Thu Oct 18 11:34  [email protected] 
                                          [email protected] 
                                          [email protected] 
                                          [email protected]

Expected View:

q9I9YP1V013809

Upvotes: 0

Views: 254

Answers (5)

Thor
Thor

Reputation: 47189

If it's always the first word, and other lines are prefixed with whitespace use:

<infile grep -Eo '^[^[:blank:]]+'

Upvotes: 0

Kent
Kent

Reputation: 195209

I would do:

awk 'NF>1{print $1}' file

since OP didn't mention any rule of the expected string. it could have '@' too.

test:

kent$  echo "q9I9YP1V013809     1472 Thu Oct 18 11:34  [email protected] 
                                          [email protected] 
                                          [email protected] 
                                          [email protected]"|awk 'NF>1{print $1}'
q9I9YP1V013809

Upvotes: 2

anubhava
anubhava

Reputation: 785631

You can check presence of @ character or base it on # of fields present as in this command:

awk 'NF > 5 { print $1 }' input.file

Upvotes: 2

January
January

Reputation: 17110

To skip all lines that start with a space, and remove everything after a space for the remaining files, do

grep -v '^ ' file.in | sed "s/ .*//"

Compared to awk, you need to start two processes, but it is much clearer what is being done.

Another solution, sed only:

sed "/^ /d;s/ .*//" file.in

Not as clear, but only one process. First sed command (/^ /d) deletes all lines that start with a space; second command is the same as above.

Upvotes: 0

Birei
Birei

Reputation: 36282

One of many ways. If first field doesn't contain an @, print it.

awk '$1 !~ /@/ { print $1; }' infile

Upvotes: 2

Related Questions