Hi All,
I have source file
x.txt
0001|0003
0031|0031
0045|0049
My desired output should be:
y.txt
0001
0002
0003
0031
0045
0046
0047
0048
0049
I had tried the below logic could you please help me out in this.....
srcpath = /temp
tgtpath= /temp
srcfilename=x.txt
tgtfilename=y.txt
cd $srcpath
filesrc=${srcpath}'/'${srcfilename}
filetgt=${tgtpath}'/'${tgtfilename}
i=1
while [ $i -le `wc -l ${filesrc} | awk '{print $1}'` ] ; do
LINE_NO = $(awk 'print NR' ${filesrc})
FROM = $(awk '{${LINE_NO}; print {$1}}' ${filesrc})
TO = $(awk '{${LINE_NO}; print {$2}}' ${filesrc})
if [ "$FROM" = "$TO" ]; then
$FROM >> ${filetgt}
else
while ( $FROM <= $TO)
do
$FROM + 1 >> ${filetgt}
done
fi
$LINE_NO+1
done
Thanks in advance
while read x; do seq -f%04g `echo $x | cut -d'|' -f1` `echo $x | cut -d'|' -f2` >> y.txt; done < x.txt
# cat y.txt
0001
0002
0003
0031
0045
0046
0047
0048
0049
Another approach:
awk -F"|" '{for(i=int($1);i<=int($2);i++)printf("%04d\n", i)}' file
birei
4
Hi kmsekhar,
One way using 'awk':
$ cat infile
0001|0003
0031|0031
0045|0049
$ cat script.awk
BEGIN {
FS = "|"
OFS = "\n"
}
{
diff = $2 - $1
while ( diff >= 0 ) {
printf "%04i\n", $2 - (diff--)
}
}
$ awk -f script.awk infile
0001
0002
0003
0031
0045
0046
0047
0048
0049
Regards,
Birei
1 Like
ctsgnb
5
just another way
while IFS='|' read a b; do printf "%04d\n" $(seq $a $b); done <yourfile
Thanks a lot for your solutions
How to store output in a file using below code?
cat script.awk
BEGIN {
FS = "|"
OFS = "\n"
}
{
diff = $2 - $1
while ( diff >= 0 ) {
printf "%04i\n", $2 - (diff--)
}
}
ctsgnb
9
while IFS='|' read a b; do seq -w $a $b; done <input >output
ctsgnb
11
Go for the "full awk" solution provided by Franklin then :
awk -F"|" '{for(i=int($1);i<=int($2);i++)printf("%04d\n", i)}' input >output
birei
12
$ awk -f script.awk infile >outfile
Regards,
Birei