-2

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 }
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
mahendra rathod
  • 1,438
  • 2
  • 15
  • 23
  • 3
    Are you matching three differently-formatted lines? What does the input data this is in reference to look like? (If you want to take the result of splitting on one delimiter, and then split *one of those fields* on another, that's a bit clearer). – Charles Duffy Aug 15 '18 at 22:04
  • 1
    Possible duplicate of [AWK multiple delimiter](https://stackoverflow.com/q/12204192/608639) – jww Aug 15 '18 at 23:15
  • 1
    Flagged for reopening. This question is about using different delimiters for different blocks. The linked question is about using multiple alternative delimiters in the same block. This is a subtle difference, but it may be important. (and the answers will be different) – viraptor Aug 16 '18 at 07:58
  • This is definitely not a duplicate of https://stackoverflow.com/q/12204192/608639. It's not a great question since it's missing sample input/output but it's not a duplicate. – Ed Morton Aug 16 '18 at 12:26

2 Answers2

4

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, ....

viraptor
  • 33,322
  • 10
  • 107
  • 191
3

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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185