简体   繁体   中英

Awk - return nth word in a column starting with pattern

I know I can use awk to get a line starting with expression exp by awk '/^exp/' . How do I get the n-th word from this line?

Use {print $n} in the awk statement

echo "abcd bcd cd d" | awk '/^ab/ { print $3}'

This would echo "cd"

give this a try:

awk -v n="$var" '/^exp/{print $n}' file

$var could be shell variable, the value is the index of your column.

awk '{print $n}'打印出该行中的第n个单词吗?

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