I am having a File format as mentioned below
Employee id|Name|Languages
12345|Hema|02|English|Hindi
4567|Basha|03|Engligh|Hindi|Telegu
the 02 and 03 are counters. Using that we need to generate the output records as mentioned below
12345|Hema|English
12345|Hema|Hindi
4567|Basha|English
4567|Basha|Hindi
4567|Basha|Telegu
Please help me to write a script.
I am trying to write using awk and having two counters as below
one counter in loop which will decriment and another counter which will be incremented for printing.
birei
2
Hi bashamsc,
Try with this perl script:
$ cat infile
12345|Hema|02|English|Hindi
4567|Basha|03|Engligh|Hindi|Telegu
$ cat script.pl
use warnings;
use strict;
die qq[Usage: perl $0 <input-file>\n] unless @ARGV == 1;
while ( <> ) {
chomp;
my @f = split /\|/;
for my $i ( 3 .. $#f ) {
printf qq[%s\n], join qq[|], @f[0..1], $f[ $i ];
}
}
$ perl script.pl infile
12345|Hema|English
12345|Hema|Hindi
4567|Basha|Engligh
4567|Basha|Hindi
4567|Basha|Telegu
Regards,
Birei
1 Like
clx
3
Another way
$ awk 'BEGIN {FS=OFS="|"}{for (i=4;i<=4+($3-1);i++) {print $1,$2,$i}}' file
12345|Hema|English
12345|Hema|Hindi
4567|Basha|Engligh
4567|Basha|Hindi
4567|Basha|Telegu
$
1 Like
$ nawk -F\| '{for(i=4;i<=NF;i++){a=$1"|"$2"|"$i}{for(i in a){print a}}}' sample.txt
12345|Hema|English
12345|Hema|Hindi
4567|Basha|Engligh
4567|Basha|Hindi
4567|Basha|Telegu
1 Like
I have written below code and it is working
while read line
do
emp_id=`echo $line | awk -F'|' '{print $1}'`
name=`echo $line | awk -F'|' '{print $2}'`
counter=`echo $line | awk -F'|' '{print $3}'`
counter1=4
while [[ $counter -gt 0 ]]
do
language=`echo $line |cut -f$counter1 -d"|"`
counter1=`expr $counter1 + 1`
counter=`expr $counter - 1`
echo $emp_id'|'$name'|'$language>>file1.txt
done
done <file.txt
Thanks for all response
If i have delimiter "|~|" instead of "|" then what will be the code.
I'd just process the data first with sed 's/|~|/|/g' data | ...
Without replacing how i can write code with awk and cut command
Without replacing how i can write code with awk and cut command