简体   繁体   English

AWK-在以pattern开头的列中返回第n个单词

[英]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/' . 我知道我可以使用awk通过awk '/^exp/'获取以表达式exp开头的行。 How do I get the n-th word from this line? 我如何从这行得到第n个字?

Use {print $n} in the awk statement 在awk语句中使用{print $ n}

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

This would echo "cd" 这将回显“ 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. $var可以是shell变量,值是列的索引。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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