Pregunta

How to extract only the string q9I9YP1V013809 from this output:

q9I9YP1V013809     1472 Thu Oct 18 11:34  test@test.com 
                                          test1@test.com 
                                          test2@test.com 
                                          test3@test.com

Expected View:

q9I9YP1V013809
¿Fue útil?

Solución

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  test@test.com 
                                          test1@test.com 
                                          test2@test.com 
                                          test3@test.com"|awk 'NF>1{print $1}'
q9I9YP1V013809

Otros consejos

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

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

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

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

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.

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

<infile grep -Eo '^[^[:blank:]]+'
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top