内嵌忽略与空间AWK [英] Ignoring embeded spaces with AWK
问题描述
我在寻找一个简单的方法来打印特定领域使用awk,同时允许在该领域的嵌入式空间。
I'm looking for a simple way to print a specific field with awk while allowing for embedded spaces in the field.
示例:字段1字段2领域中的三个字段4
我希望能够做等同于的awk'{$打印3}
,但得到三场作为一个单独的领域而不是两个。
I want to be able to do the equivalent to awk '{print $3}'
but getting "Field Three" as a single field not two.
更新:更具体地说,我需要得到后场不是$ 3,但在第3的空间被什么东西搞乱了。在$ 3引号之间的空格数是可变的。我只是想能够治疗的报价,即使不是所有的字段都引用了一个单场之间有什么。因此,忽略空格作为字段分隔符,如果引号之间。
Update: More specifically, I need to get later fields not $3 but the space in #3 is what's messing things up. The number of spaces between the quotes in $3 is variable. I'm just wanting to be able to treat what's between quotes as a single field even if not all fields are quoted. So, ignoring the spaces as field separators if between quotes.
推荐答案
可以做到这一点,如果双引号始终存在:
You can do this if the double quotes are always there:
awk -F\" '{print $2}'
具体来说,我告诉 AWK
的字段都用双引号,此时你想要的部分是现成的场2。分离
Specifically, I am telling awk
that the fields are separated by double quotes, at which point the part you want is readily available as field 2.
如果您需要获得在随后的字段,您可以分割的空间行的剩余部分,并得到一个新的数组领域,比如 F []
,这样
If you need to get at subsequent fields, you can split the remainder of the line on spaces and get a new array, say F[]
of fields, like this:
awk -F\" '{split($3,F," ");print $2,F[1],F[2]}' file
Field Three Field4 Field5
假设你的文件看起来是这样的:
assuming your file looks like this:
Field1 Field2 "Field Three" Field4 Field5 Field6
这篇关于内嵌忽略与空间AWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!