简体   繁体   中英

I'm not able to process data through awk

I'm trying to process data using awk but I'm not able to achieve the right result.Please let know If doing wrong somewhere Data:- test.txt

"A","B","ls",,"This,is,the,test",T,
"k",O,"mv",,"This,is,the,2nd test","L",
"C",J,"cd",,"This,is,the,3rd test",,

awk  'BEGIN { FS=","; OFS="|" }  { nf=0; delete f; while ( match($0,/([^,]+)|(\"[^\"]+\")/) ) { f[++nf] = substr($0,RSTART,RLENGTH); $0 = substr($0,RSTART+RLENGTH); };  print f[2],f[3],f[4],f[5] }' test.txt 

Ouput

"B"|"ls"|"This,is,the,test"|T
O|"mv"|"This,is,the,2nd test"|"L"
J|"cd"|"This,is,the,3rd test"|

But output should be like this

"B"|"ls"||"This,is,the,test"|T
O|"mv"||"This,is,the,2nd test"|"L"
J|"cd"||"This,is,the,3rd test"|
awk -F\" '{q="\""; print q$4q"|"q$6q"||"q$8q}'
awk -vFPAT='"[^"]*"' '{$0=$2"|"$3"||"$4}1' FILE

使用拍

With your new input and any awk:

$ cat tst.awk
BEGIN { FS=","; OFS="|" }
{
    # 1) Replace all FSs inside quotes with the value of RS
    #    since we know that RS cannot be present in any record:
    head = ""
    tail = $0
    while( match(tail,/"[^"]+"/) ) {
        trgt = substr(tail,RSTART,RLENGTH)
        gsub(FS,RS,trgt)
        head = head substr(tail,1,RSTART-1) trgt
        tail = substr(tail,RSTART+RLENGTH)
    }
    $0 = head tail

    # 2) re-compile the record to replace FSs with OFSs:
    $1 = $1

    # 3) restore the RSs within quoted fields to FSs:
    gsub(RS,FS)

    # 4) remove the first and last fields:
    gsub("^[^" OFS "]*[" OFS "]|[" OFS "][^" OFS "]*$","")

    print
}

$ awk -f tst.awk file
"B"|"ls"||"This,is,the,test"|T
O|"mv"||"This,is,the,2nd test"|"L"
J|"cd"||"This,is,the,3rd test"|

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM