Pivot using awk

Hi,

I am writing a code to basically pivot the data.

awk -v var1="" -v var2="" -v var3="" -v var4="" -v var5="" -v Disp=0\
'BEGIN {FS=":"; OFS="|";}\
/^Pattern1/ {var1=$2;Disp=0;} \
/^Pattern2/ {var2=$2;} \
/^Pattern3/ {var3=$2;} \
/^Pattern4/ {var4=$2;} \
/^Pattern5/ {var5=$2;Disp=1;} \
{
print "$var1,$var2,$var3,$var4,$var5"
}' "InputFile" "OutputFile"

But I am getting the below issues
a) $2 is picking the entire record
b) print statement is displaying result as below

c) How can we remove leading and trailing spaces of Value1... Value5

  1. awk != perl
  2. var != $var

Can't you use something simple like:

awk '{print $NF}' file | paste -sd \| -

Or is the output not always one line? Are your sample representative for your actual input and output ?

Try also

awk '{OUT=OUT DELIM $NF; DELIM="|"} END {print OUT}' file
Value1|Value2|Value3|Value4|Value5

and this ???

 awk -F"[: ]" 'BEGIN{ORS="|";}{print $NF}' file

That works too, but the problem is that the last character is a pipe symbol instead of a newline.