如何定义在常规的前pression(AWK中)的空间? [英] how to define a space in a regular expression (in awk)?

查看:131
本文介绍了如何定义在常规的前pression(AWK中)的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印里面的文本。比如我有以下字符串:

I want to print the texts inside of " ". for example I have the following strings:

gfdg "jkfgh" "jkfd fdgj fd-" ghjhgj
gfggf "kfdjfdgfhbg" "fhfghg" jhgj
jhfjhg "dfgdf" fgf
fgfdg "dfj jfdg jhfgjd" "hfgdh jfdhgd jkfghfd" hgjghj

和我想只打印以下内容:

And I want to print only the following:

"jkfgh" "jkfd fdgj fd-"
"kfdjfdgfhbg" "fhfghg"
"dfgdf"
"dfj jfdg jhfgjd" "hfgdh jfdhgd jkfghfd"

我曾与下面的正前pression awk的尝试:

I have tried awk with the following regular expression:

awk '{for(i = 1; i <= NF; i++) if($i ~ /^\"[A-Za-z.$]*([A-Za-z.$][[:space:]]*[A-Za-z.$])*\"$/) print $i}' sample.txt

但它打印空间之前的一切,居然不承认我在常规的前pression定义的空间。我的电流输出是:

but it prints everything before space and actually does not recognize the spaces I have defined in my regular expression. My current output is:

"jkfgh"
"kfdjfdgfhbg" "fhfghg"
"dfgdf"
"dfj

你可以看到,只有没有任何空间的那些正确打印。

as you can see, only the ones without any space are printed correctly.

我也试过 [:空白:]] \\ t ,但没有奏效。

I have also tried [[:blank:]], \t and also ' ' but did not work.

我AP preciate如果有人能告诉我如何改变这种常规的前pression,包括空间。

I appreciate if someone can tell me how to change this regular expression and include space.

推荐答案

您是刚开始那些没有任何空间,因为你遍历领域,他们是空格分隔。因此,你需要的方式更改为操作的空间不同。假设没有嵌套的报价,你可以使用,例如:

You are just getting those without any space because you loop through fields and they are space separated. Thus, you need to change the approach to something handling the spaces differently. Assuming there are no nested quotes, you can use for example:

awk -F'"' '{for (i=2;i<NF;i+=2) printf "\"%s\"", $i; print ""}' file

也就是说,使用作为字段分隔符和打印偶数场。

That is, use " as field separator and print the even fields.

这是等同于使用 FS 更优雅的:

This is equivalent to using FS more elegantly:

awk -F'"' '{for (i=2;i<NF;i+=2) printf "%s%s%s", FS, $i, FS; print ""}' file

请注意在previous接近输出在字段之间没有空格。如果你需要它,你可以使用:

Note in the previous approaches the output has no space in between fields. If you need it, you can use:

awk -F'"' '{for (i=2;i<NF;i+=2) printf "%s%s%s%s", FS, $i, FS, (i>NF-2?"\n":" ")}' file

诀窍(I&GT; NF-2\\ n:)是一个分离器打印整个领域共同的问题。如果我们在最后一个字段,所以把它作为新的生产线;否则,作为一个空间。更惯用,你也可以说(I&GT; NF-2 RS:OFS)使用 RS 的默认值(记录分隔符,新行)和 OFS (输出字段分隔符,空格)。

The trick (i>NF-2?"\n":" ") is a matter of printing the whole field together with a separator. If we are in the last field, we set it as new line; otherwise, as a space. More idiomatically, you can also say (i>NF-2?RS:OFS) using the default values of RS (record separator, new line) and OFS (output field separator, space).

$ awk -F'"' '{for (i=2;i<NF;i+=2) printf "%s%s%s%s", FS, $i, FS, (i>NF-2?"\n":" ")}' file
"jkfgh" "jkfd fdgj fd-"
"kfdjfdgfhbg" "fhfghg"
"dfgdf"
"dfj jfdg jhfgjd" "hfgdh jfdhgd jkfghfd"

这篇关于如何定义在常规的前pression(AWK中)的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆