简体   繁体   中英

awk + print the field from line that defined with double quotes

how to print the third field from line that defined with double quotes

for example

echo activate.bash /home/adm/run.sh "run on machine with eth2 active" "94061" | awk '{print $3}'

I expected that the awk will print the third filed in the double quotes

 run on machine with eth2 active

but its print

 run

so what's I need to fix in my ksh syntax?

other example

echo 123 123 "1 2 3 4 5" | awk '{print $3}'

should print

1 2 3 4 5

You can use custom field separator in awk like this:

s='activate.bash /home/adm/run.sh "run on machine with eth2 active" "94061"'
echo "$s" | awk -F '"' '{print $2}'
run on machine with eth2 active

PS: Better to use:

awk -F '"' '{print $2}' <<< "$s"

Working Code Demo

Let's take the following example:

 cat numbers
 123 123 "run on machine with eth2 active" "94061"


cat numbers | awk -F '"' 'BEGIN{OFS=" ";} {print $2;}'

OUTPUT: run on machine with eth2 active

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM