简体   繁体   中英

How can I cut a string in bash ignoring escaped delimiters?

I am cutting a string from a csv file using comma as delimiter. Is there a way to add an option to the delimiter to ignore certain strings containing the delimiter?

I want to ignore all commas that are written inside quotation marks.

Example:

    asdf,1337,"asdf, asdf"

should become

    asdf
    1337
    asdf, asdf

and NOT

    asdf
    1337
    "asdf,
     asdf"

This awk codes should help:

awk '{$0=$0","; while($0) { match($0,/ *"[^"]*" *,|[^,]*,/); 
             field=substr($0,RSTART,RLENGTH);
             gsub(/^ *"?|"? *,$/,"",field); 
             print field; $0=substr($0,RLENGTH+1)  }}' file 

with your example:

kent$ echo 'asdf,1337,"asdf, asdf"'|awk '{$0=$0",";
while($0) {
  match($0,/ *"[^"]*" *,|[^,]*,/)
  field=substr($0,RSTART,RLENGTH)
  gsub(/^ *"?|"? *,$/,"",field) 
  print field
  $0=substr($0,RLENGTH+1)  
}}'
asdf
1337
asdf, asdf

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