我如何使用awk打印多个定界词? [英] how i could print multiple delimiter word using awk?

查看:77
本文介绍了我如何使用awk打印多个定界词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有正则表达式分隔符的awk,我需要提取名和姓的单词,但是此命令无效

I have a awk with regex delimiter I need to extract word of firstname and last name , but this command not works

        awk -v    OFS="\t" -v FS='firstName": "|",[^+]*lastName": "|",   "' '{sum[$1]+=$2;} {print  $1,$2}' sumacomando

"firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",

我需要打印

gdrgo,222
beto,111
beto,111
beto2,555
beto2,444
gdrgo,222
beto2,444

请帮助我

推荐答案

您的输入为CSV,因此处理该输入的方法为

Your input is a CSV so the way to handle that is GNU awk for FPAT, and your output is also a CSV so setting OFS to a tab wouldn't make sense. In general when you have ANY file that contains name->value mappings the simplest, most robust way to handle it is to first create an array of those mappings (f[] below) and then you can print or otherwise manipulate your data by using it's names:

$ cat tst.awk
BEGIN {
    FPAT = "([^,]*)|(\"[^\"]+\")"
    OFS = ","
}
{
    delete f
    for (i=1; i<=NF; i++) {
        split($i,t,/[[:space:]":]+/)
        f[t[2]] = t[3]
    }
    print f["firstName"], f["lastName"]
}

$ awk -f tst.awk file
gdrgo,222
beto,111
beto,111
beto2,555
beto2,444
gdrgo,222
beto2,444

这篇关于我如何使用awk打印多个定界词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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