简体   繁体   中英

Can I use a different delimiter in different code blocks in awk?

In AWK can i set up multiple delimiter in action , like below example

BEGIN { Actions}
{ACTION-1} # here delimiter will be comma ","
{ACTION-2} # here delimiter will be colon":"
{ACTION-1} # here delimiter will be space " "
END { Actions }

Not really, but what you can do instead is explicitly split the line you're looking at. That would be:

{ split($0, parts, ","); ACTION-1 }
{ split($0, parts, ":"); ACTION-2 }
{ split($0, parts, " "); ACTION-1 }

This way you get the values you're after, but they're accessible as parts[1], parts[2], ... rather than $1, $2, ... .

Yes you can:

BEGIN { Actions}
{FS=","; $0=$0; ACTION-1} # here delimiter will be comma ","
{FS=":"; $0=$0; ACTION-2} # here delimiter will be colon":"
{FS=" "; $0=$0; ACTION-1} # here delimiter will be space " "
END { Actions }

Assigning anything to $0 causes awk to re-do field splitting using the current value of FS so the fact it had already split the record into fields before then is irrelevant.

Having said that - there's almost certainly a better approach to whatever it is you're trying to do.

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