bash 如何使用“:”作为 awk 字段分隔符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2609552/
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-09 19:09:24  来源:igfitidea点击:

How to use ":" as awk field separator?

bashawkgawk

提问by user173446

Given following command:

给出以下命令:

echo "1: " | awk '/1/ -F ":" {print }'

why does awk output:

为什么 awk 输出:

1: 

回答by Jürgen H?tzel

"-F" is a command line argument not awk syntax, try:

"-F" 是命令行参数而不是 awk 语法,请尝试:

 echo "1: " | awk -F  ":" '/1/ {print }'

回答by Paused until further notice.

If you want to do it programatically, you can use the FSvariable:

如果要以编程方式执行此操作,可以使用FS变量:

echo "1: " | awk 'BEGIN { FS=":" } /1/ { print  }'

Note that if you change it in the main loop rather than the BEGINloop, it takes affect for the nextline read in, since the current line has already been split.

请注意,如果您在主循环中而不是在BEGIN循环中更改它,则会对读入的下一行产生影响,因为当前行已被拆分。

回答by fedorqui 'SO stop harming'

You have multiple ways to set :as separator:

您有多种方法可以设置:为分隔符:

awk -F: '{print }'

awk -v FS=: '{print }'

awk '{print }' FS=:

awk 'BEGIN{FS=":"} {print }'

All of them are equivalent and for an will return 1for a sample input "1:2:3":

它们都是等价的,并且会返回1样本输入“1:2:3”:

$ awk -F: '{print }' <<< "1:2:3"
1
$ awk -v FS=: '{print }' <<< "1:2:3"
1
$ awk '{print }' FS=: <<< "1:2:3"
1
$ awk 'BEGIN{FS=":"} {print }' <<< "1:2:3"
1

回答by danben

-Fis an argument to awkitself:

-F是对awk自身的论证:

$echo "1: " | awk -F":" '/1/ {print }'
1

回答by Zlemini

You can also use a regex as a field separator, the following will print "bar" by using a regex to set the number "10" as a separator.

您也可以使用正则表达式作为字段分隔符,以下将通过使用正则表达式将数字“10”设置为分隔符来打印“bar”。

echo "foo 10 bar" | awk -F'[0-9][0-9]' '{print }'

回答by Bhavuk Taneja

No Need to write this much. Just put your desired field separator with -F option in awk command and the column number you want to print segregated as per your mentioned field separator.

没必要写这么多。只需在 awk 命令中使用 -F 选项放置所需的字段分隔符,并根据您提到的字段分隔符将要打印的列号分开。

echo "1: " | awk -F: '{print }'    
1

echo "1#2" | awk -F# '{print }'  
1

回答by jihed gasmi

AWK works as text interpreter that goes linewise for the whole documentand that goes fieldwise for each linethus $1,$2..$n are references to the fields of each line($1 is the first field,$2 is the second field and so on...). You can define a field separator by using the "-F" switch under the command line or within two brackets with "FS=...". Now consider the answer of "JUERGEN" :

AWK作品文本翻译是去面向行整个文档那去fieldwise每行因此$ 1,$ 2 .. $ n的每一行的字段引用($ 1是第一个字段,$ 2是第二场等等……)。您可以使用命令行下的“-F”开关或在带有“FS=...”的两个括号内使用“-F”开关来定义字段分隔符。现在考虑“JUERGEN”的答案:

echo "1: " | awk -F  ":" '/1/ {print }'

Above the field boundaries are set by ":" so we have two fields $1 which is "1" and $2 which IS the empty space.After, comes the regular expression "/1/" that instructs the filter to output the first field only when the interpreter stumbles upon a line containing such an expression(i mean 1); The output of the "echo" command is one line that contains "1" so the filter will work...

字段边界上方由 ":" 设置,因此我们有两个字段 $1,即 "1" 和 $2,即空白区域。之后是正则表达式 "/1/",指示过滤器仅输出第一个字段当解释器偶然发现一行包含这样的表达式时(我的意思是 1);“echo”命令的输出是包含“1”的一行,因此过滤器将工作......

When dealing with the following example :

处理以下示例时:

echo "1: " | awk '/1/ -F ":" {print }'

The syntax is messy and the interpreter chose to ignore the part F ":"and switches to the default field splitter which is the empty space thus outputting "1:" as the first field and there will be not a second field!

语法很乱,解释器选择忽略 F 部分“:”并切换到默认字段拆分器,即空白区域,因此输出“1:”作为第一个字段,不会有第二个字段!

The answer of JUERGEN contains the good syntax...

JUERGEN 的答案包含良好的语法...

回答by Vonton

Or you can use:

或者你可以使用:

echo "1: " | awk  '/1/{print -":"}' 

This is really funny equation.

这真是一个有趣的等式。