Tom
Tom

Reputation: 173

awk split field format

awk '{split($2,a,"-")};
     {if ($1=="aa") {print "'$var'/"$1"/"a[1]"/"$2" '$var'/bb/"a[1]} 
      else {print "'$var'/"$1"/"a[1]"/"$2" '$var'/aa/"a[1]}}'

hi, $2 is all lower case text. I have split it and would like to output a[1] in upper case.

How can I do this ?

Tom

Upvotes: 1

Views: 812

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 359875

You can avoid complex and hard to read quoting if you use AWK's variable passing. You can also make use of the Output Field Separator. Also, you have extra curly braces that you don't need.

awk -v "var=$var" 'BEGIN{OFS="/"}
  {split($2,a,"-"); a[1]=toupper(a[1])
  if ($1=="aa") {print var, $1, a[1], $2 " " var, "bb", a[1]} 
    else {print var, $1, a[1], $2 " " var, "aa", a[1]}}'

Upvotes: 0

kurumi
kurumi

Reputation: 25599

you can use awk's toupper(a[1])

Upvotes: 3

Related Questions