Hello Everybody,
I have a file with data like this
8z:1
15y:0
7x:0
12w:1
...
...
I would like read each line through a loop and will needing to get the input from each line like this
For 8z:1; a=8,b=1
15y:0; a=15,b=0
7x:0; a=7,b=0
Please let me know of a way to do this
Thanks in advance!!!
birei
February 21, 2011, 3:03am
2
Hi,
Not sure about your output, but try this:
$ perl -aF: -ne '$input = join(":", @F); chomp $input; print "For $input", "; ", "a=", int($F[0]), ",", "b=", int($F[1]), "\n";' infile
For 8z:1; a=8,b=1
For 15y:0; a=15,b=0
For 7x:0; a=7,b=0
For 12w:1; a=12,b=1
Regards,
Birei
1 Like
Try something like:
while IFS=: read a b
do
a=${a%?}
echo $a
echo $b
done < file
1 Like
Ygor
February 21, 2011, 3:09am
4
Try something like...
$ echo '8z:1
> 15y:0
> 7x:0
> 12w:1' | while read my_row
> do
> eval $(echo $my_row|awk -F '[^0-9]:' '{print "a=" $1 ORS "b=" $2}')
> echo do something with $a and $b
> done
do something with 8 and 1
do something with 15 and 0
do something with 7 and 0
do something with 12 and 1
1 Like
here is the basic logic . here you can get two variables $a and $b for every line. You can modify it as per your requirement
while read line
do
a=`echo $line |awk -F: '{print $1 }'`
b=`echo $line |awk -F: '{print $2 }'`
a=`echo $a | tr -d '[:alpha:]'`
b=`echo $b | tr -d '[:alpha:]'`
echo "a=$a,b=$b"
done < $inputfilename
1 Like
Options working great..Thanks everyone!!