Conversion of '|' delimiter

Hello All,

I want to convert the data saparated by '|' delimited in the flat file to the readable format.

The every odd number '|' (pipe) to be converted to '\tab' and every even numbered '|' to '\n' and transfer the data to another file.

example.

NAME|ABC|LASTNAME|PQR|AGE|20|SEX|MALE|
NAME|XYZ|LASTNAME|PDF|AGE|21|SEX|MALE|

output should be tab delimited as below -

NAME    ABC
LASTNAME   PQR
AGE      20
SEX      MALE
 
NAME    XYZ
LASTNAME   PDF
AGE      21
SEX      MALE    etc....

I tried the below script first to convert every second '|' to '\n' line but it's not working.

#!/usr/bin/ksh
FileName=INPUT_FILE.txt

if [[ -e $FileName ]]; then
while read line
  do
    awk -F |  '{for( i = 1;i <= NF;++i)
      if( i % 2 == 0 ) printf("%s\n",$i) 
    else
      printf("%s|",$i) ; }'
  done < $FileName
else
   echo "File $FileName  does not exists"
fi

Thank you in advance.

Let's do it with sed:

sed ':a;s/|/ /;s/|/\n/;ta' infile

I am getting message as 'Label too long : :a;s/|/ /;s/|/\n/;ta'

The each input line will be more than 1000 characters and contains more than 75 even numbers of '|' delimiter.

$ cat cmd
:a
s/|/ /
s/|/\
/
t a
$ sed -f cmd file
NAME ABC
LASTNAME PQR
AGE 20
SEX MALE

NAME XYZ
LASTNAME PDF
AGE 21
SEX MALE

using perl

perl -wpl -e 'while (/\|/) { ++$i ; if ($i%2) {s/\|/\t/}  else {s/\|/\n/} ; }'infile.txt

;);):wink:

Hi Ahmad,

It's working:b:. Could you please explain how it works ?

Does this work (or Anbu's alternative)?

sed -e :a -e 's/|/ /' -e 's/|/\n/' -e ta infile

Fix your code.

#!/usr/bin/ksh
FileName=INPUT_FILE.txt

if [[ -e $FileName ]]; then
  awk -F "|"  '{for( i = 1;i <= NF;++i)
      if( i % 2 == 0 ) printf("%s\n",$i)
      else
        printf("%s\t",$i) ; }{print ""}' $FileName
else
   echo "File $FileName  does not exists"
fi

The above mentioned code is converting the first '|' to Tab delimited but unable to format the second '|' value to new line.

Also, I found another issue is like the input file does not contains the '|' at the end of each line hence when I execute the file using Ahmad's code, the format is getting changed from second line.

The actual data is coming like this -

NAME|ABC|LASTNAME|PQR|AGE|20|SEX|MALE
NAME|XYZ|LASTNAME|PDF|AGE|21|SEX|MALE

My code are not changed for your new INPUT_FILE.txt, but still get correct result.

$ cat INPUT_FILE.txt
NAME|ABC|LASTNAME|PQR|AGE|20|SEX|MALE
NAME|XYZ|LASTNAME|PDF|AGE|21|SEX|MALE

$ cat ssachins.sh
#!/usr/bin/ksh
FileName=INPUT_FILE.txt

if [[ -e $FileName ]]; then
  awk -F "|"  '{for( i = 1;i <= NF;++i)
      if( i % 2 == 0 ) printf("%s\n",$i)
      else
        printf("%s\t",$i) ; }{print ""}' $FileName
else
   echo "File $FileName  does not exists"
fi

$ ./ssachins.sh
NAME    ABC
LASTNAME        PQR
AGE     20
SEX     MALE

NAME    XYZ
LASTNAME        PDF
AGE     21
SEX     MALE

I tried the above code too but getting the below error -

awk: syntax error near line 1
awk: bailing out near line 1

Use nawk or /usr/xpg4/bin/awk on Solaris.

It's working now.
Thank you all.