okdev
October 4, 2007, 7:43pm
1
Hello Friends,
I have file called reference.txt which looks like below,
columns are been formed by delimiter "|" as shown. I am required to scan whole file line by line & compare 1st 2 columns with new name (ex. ferrari) & new version (2.1).
CODE PURPOSE: whnever new project formed compare if it already exists & compare if version updated.
My Code
for file in $(cat reference.txt);do
if [[ `echo $file | cut -d ',' -f1` == $PROJECT ]];then
echo "Project Matched"
if [[ `echo $file | cut -d ',' -f2` == $VERSION ]];then
echo "Project & Version Matched"
else
echo "Project Matched but Version Revised"
fi
else
echo "Complete New Project"
fi
echo "$n"
let "n += 1"
done
By executing my code i am finding,
its looping for 19 times instead of looping for only 3 times. & providing 3 output for 3 rows.
Kindly help. Thanks a lot.
Ygor
October 4, 2007, 8:54pm
2
Perhaps try something like this....
oldIFS="$IFS"
IFS='|'
while read f1 f2 etc
do
echo do something with $f1 and $f2
done < reference.txt
IFS="$oldIFS"
okdev
October 4, 2007, 10:00pm
3
Sir thanks a lot for this quick solution. It absolutely gonna help. But stay connected.. if i need some more help!!!
Thanks
okdev
October 5, 2007, 4:55pm
4
Once i find PROJECT matched & project VERSION matched. I am supposed to update $f3 & $f4 (3rd & 4th field) of same file.
Example:
reference.txt
New Project with following variables (I have)
Reference.txt should look like follwing.
Code:
#!/bin/sh
PROJECT=ferrari12
VERSION=4.0
LABLE=ferrari12-1.1.25
n=1 # Line Number
oldIFS="$IFS"
IFS='|'
while read f1 f2 f3 etc
do
if [ $f1 == $PROJECT ];then
echo "Line $n: Project Matched"
if [ $f2 == $VERSION ];then
echo " Version Matched"
sed 's/$f3/$LABLE/' # Lable Replaced
else
echo " Version Not Matched"
fi
else
echo "Line $n: New Project"
fi
let "n += 1"
done < reference.txt
IFS="$oldIFS"
some how sed is not working for me there to replace lable. Do you have some logic to fit in this loop.
Please Help! Thanks
okdev
October 6, 2007, 7:45pm
5
People Please Help me out.
Thanks:)
drl
October 6, 2007, 9:30pm
6
Hi.
Use double quotes in this situation -- single quotes do not allow variable expansion:
sed 's/$f3/$LABLE/' # Lable Replaced
I suggest you look over some tutorials like BASH Programming - Introduction HOW-TO ... cheers, drl
sed "s/$f3/$LABle/"
cheers,
Devaraj Takhellambam