通过awk读取文件 [英] File read through awk

查看:63
本文介绍了通过awk读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件包含下面的行,就像我想通过awk管理

I have file which contain line below like I want to manage through awk

文件名:-test.txt

filename:-test.txt

"A","@900",9999,"Test Place","Quayside Sc, Sligo, Tel: 071 9154382","SCRIPT",20150317

我要通过单个字符串"Quayside Sc,Sligo,Tel:071 9154382"来管理

I want to manage this is as single string "Quayside Sc,Sligo, Tel: 071 9154382"

当我执行以下命令时,它会自动在逗号前获取第一个字符串

It automatically take first string before comma when I perform following command

echo "A","@900",9999,"Test Place","Quayside Sc, Sligo, Tel: 071 9154382","SCRIPT",20150317 | awk -F ',' '{ print $4 "|" $8 }'
Test Place|SCRIPT

推荐答案

在gnu-awk中使用FPAT,您可以将整个引用的字符串作为单个字段获取:

Using FPAT in gnu-awk you can get whole quoted string as single field:

awk 'BEGIN{ FPAT="\"[^\"]*\"|[^,]*" } {print $4 ORS $5}' file
"Test Place"
"Quayside Sc, Sligo, Tel: 071 9154382"

FPAT="\"[^\"]*\"|[^,]*"使用正则表达式分解用引号引起来或用逗号分隔的字段.

FPAT="\"[^\"]*\"|[^,]*" uses a regex to break down fields surrounded by quotes or separated by comma.

出于演示目的,这里是每个已解析的字段:

For demo purpose here is each parsed field:

awk 'BEGIN{ FPAT="\"[^\"]*\"|[^,]*" } {for (i=1; i<=NF; i++) {
         printf "$%d: <%s>\n", i, $i}}' file
$1: <"A">
$2: <"@900">
$3: <9999>
$4: <"Test Place">
$5: <"Quayside Sc, Sligo, Tel: 071 9154382">
$6: <"SCRIPT">
$7: <20150317>


更新:如果您没有gnu-awk 4,则可以使用此perl命令以达到相同的效果:


Update: If you don't have gnu-awk 4 then you can use this perl command for same effect:

perl -F',(?=(?:(?:[^\"]*\"){2})*[^\"]*$)' -ane 'print $F[3] . "\n" . $F[4] . "\n"' file
"Test Place"
"Quayside Sc, Sligo, Tel: 071 9154382"

这篇关于通过awk读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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