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
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.
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