Eugeniu Rosca
Eugeniu Rosca

Reputation: 5305

Split string in AWK using multi-character delimiter

I have the following delimiter and string:

DEL=":::"
STR="info1"$DEL"info2"$DEL"info3"

I want to extract info1-2-3 from STR using awk.

The following works:

info1=$(echo $STR | awk '{split($0,a,":::")} END{print a[1]}')
info2=$(echo $STR | awk '{split($0,a,":::")} END{print a[2]}')
info3=$(echo $STR | awk '{split($0,a,":::")} END{print a[3]}')

The following does NOT work:

info1=$(echo $STR | awk '{split($0,a,"$DEL")} END{print a[1]}')

What's wrong?

Upvotes: 2

Views: 5069

Answers (1)

SMA
SMA

Reputation: 37023

Since DEL is your shell variable, you should be using something like:

info1=$(echo $STR | awk -v delimeter="$DEL" '{split($0,a,delimeter)} END{print a[1]}')

Upvotes: 4

Related Questions