[英]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.