bash 嵌套的 awk 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23763046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:29:50  来源:igfitidea点击:

Nested awk command

bashshellawk

提问by jmlemetayer

I have a file with this syntax:

我有一个具有以下语法的文件:

foo:bar:value1,value2,value3,...:foo:bar

I want to get the value list (space separated) which does not have a fixed length:

我想获取没有固定长度的值列表(空格分隔):

value1 value2 value3 ...

Is there a way to do this in a single awk command ? Instead of piping commands like that:

有没有办法在单个 awk 命令中做到这一点?而不是像这样的管道命令:

awk -F: '{print }' myfile.txt | awk -F, '{OFS=" ";  = ; print 
awk -F: '{gsub(/,/," ",);print }' file
}'

回答by jaypal singh

If you know which field to grab the csv separated values, then you can use gsubfunction too:

如果您知道要获取 csv 分隔值的字段,那么您也可以使用gsub函数:

$ cat file
foo:bar:value1,value2,value3,value3:foo:bar
foo:bar:value1,value2:foo:bar
foo:bar:value1,value2,value3,value4,value5,value6:foo:bar

$ awk -F: '{gsub(/,/," ",);print }' file
value1 value2 value3 value3
value1 value2
value1 value2 value3 value4 value5 value6

awk -F : '{ split(, a, ","); print a[1], a[2], a[3] }' file

回答by tripleee

You can use Awk's split()function to do additional splitting.

您可以使用 awk 的split()函数进行额外的拆分。

##代码##