Mukesh
Mukesh

Reputation: 25

How do I use multiple character as single delimiter in awk?

I have a target file with delimiter as "|^/".

Example :

123|^/qwe|^/po9076|^/env12|^/opti|^/546454

Now the requirement is to get the count of fields in each line.

I tired something like below, but it did not give correct result.

echo "123|^/qwe|^/po906|^/env12|^/opti|^/54644" | awk -F'|^/' '{print NF; exit}'

How to go about this issue?

Note: There can be either | or ^ or / inside a field value. So the delimiter should be combination of |^/.

Upvotes: 1

Views: 1548

Answers (2)

Ed Morton
Ed Morton

Reputation: 203209

$ echo "123|^/qwe|^/po906|^/env12|^/opti|^/54644" | awk -F'\\|\\^/' '{print NF; exit}'
6

man awk and google regular expressions.

Upvotes: 2

anubhava
anubhava

Reputation: 784958

You should use a brace expression (or character class) as field separator:

echo "123|^/qwe|^/po906|^/env12|^/opti|^/54644" | awk -F '[|^/]+' '{print NF; exit}'

6

Regex [|^/]+ will make one or more of given characters inside [...] as input field separator.

Upvotes: 0

Related Questions