阅读背景:

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

来源:互联网 

Given following command:

鉴于以下命令:

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

Given following command:

鉴于以下命令:

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

why does awk output:

为什么awk输出:

1: 

8 个解决方案

#1


251  

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

“-F”是一个命令行参数,而不是awk语法,请尝试:

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

#2


46  

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

如果你想编程,你可以使用FS变量:

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

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

注意,如果在主循环中而不是开始循环中更改它,则会影响下一行的读入,因为当前行已经被拆分了。

#3


12  

-F is an argument to awk itself:

-F是一个关于awk本身的论证:

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

#4


7  

You have multiple ways to set : as separator:

您有多种方式来设置:作为分隔符:

awk -F: '{print $1}'

awk -v FS=: '{print $1}'

awk '{print $1}' FS=:

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

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

它们都是等价的,对于一个样本输入“1:2:3”的返回值为1。

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

#5


4  

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.

您还可以使用regex作为字段分隔符,下面将使用regex将数字“10”设置为分隔符,从而打印“bar”。

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

#6


3  

AWK works as text interpreter that goes linewise for the whole document and that goes fieldwise for each line thus $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是一个文本解释器,它在整个文档中是线性的,并且每一行的字段值为$1,$2。$n是对每一行的字段的引用($1是第一个字段,$2是第二个字段,等等)。您可以使用命令行下的“-F”开关或在两个方括号内使用“FS=…”来定义字段分隔符。现在考虑一下“JUERGEN”的答案:

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

Above the field boundries are set by ":" so we have two fileds $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 stumble upon a line containing such expression(i mean 1); The output of the "echo" command is one line that containes "1" so the filter will work...

在字段边界上设置为“:”,因此我们有两个fileds $1,即“1”和$2,这是空白空间。之后,正则表达式“/1/”指示筛选器只在解释器遇到包含该表达式的一行时才输出第一个字段(我的意思是1);“echo”命令的输出是一个包含“1”的行,因此过滤器将工作……

When dealing with the following example :

在处理以下示例时:

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

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

语法混乱,解释器选择忽略F部分“:”并切换到默认的字段分割器,这是空的sapce,因此输出“1:”作为第一个字段,将不会有第二个字段!

The answer of JUERGEN contains the good syntax...

JUERGEN的答案包含了良好的语法。

#7


2  

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.

没必要写这么多。只要将你想要的字段分隔符和-F选项在awk命令中,然后按你所提到的字段分隔符将你想要打印的列号分隔开。

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

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

#8


1  

Or you can use:

或者你可以使用:

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

This is really funny equation.

这是一个非常有趣的等式。


}' echo "1: "



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: