Adding a column

Hello ,

My requirement is to add additional column sequentially to a text file based on the column value -

i/p file

id1|varchar
id2|varchar
id3|number
id4|number
id5|date
id6|date

---------------------------------------
o/p file

colv1|id1 (if second column value is varchar then first value is colv then sequence)
similarly
colv2|id2
coln1|id3(if second column value is number then first value is coln then sequence)
similarly
coln2|id4
cold1|id5(if second column value is date then first value is cold then sequence)
similarly
cold2|id6

so the o/p will be

colv1|id1
colv2|id2
coln1|id3
coln2|id4
cold1|id5
cold2|id6

Please suggest , thanks!!

With (Posix-) shell internals

nv=0; nn=0; nd=0; while IFS="|" read c1 c2
do
  case $c2 in
  (varchar) x=v; seq=$((nv+=1));;
  (number) x=n; seq=$((nn+=1));;
  (date) x=d; seq=$((nd+=1));;
  (*) x=""; seq=0;;
  esac
  echo "col$x$seq|$c1"
done < input
1 Like

Try:

awk '{s=substr($2,1,1); print "col" s (++A), $1}' FS=\| OFS=\| file

A Perl alternative:

perl -ple 's/^(.*)\|(\w).*/"col$2".++$n{$2}."|$1"/e' pratik4891.file
colv1|id1
colv2|id2
coln1|id3
coln2|id4
cold1|id5
cold2|id6