The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem in For Loop The Observer Shell Programming and Scripting 2 05-28-2008 03:43 AM
Kindly help : sort charandevu Shell Programming and Scripting 1 04-27-2008 07:40 AM
comparing 2 files - kindly assist expert chew Shell Programming and Scripting 15 03-24-2008 04:59 AM
Can someone kindly help with appending problem!!! cyberray Shell Programming and Scripting 10 10-24-2007 07:15 AM
problem with while loop mridula High Level Programming 1 12-11-2005 11:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2007
okdev's Avatar
okdev okdev is offline
Registered User
  
 

Join Date: Sep 2007
Location: Dallas, Texas
Posts: 5
FOR loop problem: Kindly Help

Hello Friends,

I have file called reference.txt which looks like below,

Quote:
wahabeua |3.1 |wahabeua-1.1.11 |Not available |Code Coverage
ferrari12 |4.0 |ferrari12-1.1.21|Not applicable |Code Coverage
masterpiece |4.1 |masterpiece-2.2.33 |File Changes |Code Coverage
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
Quote:
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,
1. its looping for 19 times instead of looping for only 3 times. & providing 3 output for 3 rows.

Kindly help. Thanks a lot.

Last edited by okdev; 10-04-2007 at 08:55 PM..
  #2 (permalink)  
Old 10-04-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Perhaps try something like this....
Code:
oldIFS="$IFS"
IFS='|'
while read f1 f2 etc
do
   echo do something with $f1 and $f2
done < reference.txt
IFS="$oldIFS"

  #3 (permalink)  
Old 10-04-2007
okdev's Avatar
okdev okdev is offline
Registered User
  
 

Join Date: Sep 2007
Location: Dallas, Texas
Posts: 5
Thanks But Keep in touch

Sir thanks a lot for this quick solution. It absolutely gonna help. But stay connected.. if i need some more help!!!



Thanks
  #4 (permalink)  
Old 10-05-2007
okdev's Avatar
okdev okdev is offline
Registered User
  
 

Join Date: Sep 2007
Location: Dallas, Texas
Posts: 5
Continuation Problem

Once i find PROJECT matched & project VERSION matched. I am supposed to update $f3 & $f4 (3rd & 4th field) of same file.

Example:
reference.txt
Quote:
wahabeua |3.1 |wahabeua-1.1.11 |Not available |Code Coverage
ferrari12 |4.0 |ferrari12-1.1.21|Not applicable |Code Coverage
masterpiece |4.1 |masterpiece-2.2.33 |File Changes |Code Coverage
New Project with following variables (I have)
Quote:
PROJECT=ferrari12
VERSION=4.0
LABLE=ferrari12-1.1.25
Reference.txt should look like follwing.

Quote:
wahabeua |3.1 |wahabeua-1.1.11 |Not available |Code Coverage
ferrari12 |4.0 |ferrari12-1.1.25|Not applicable |Code Coverage
masterpiece |4.1 |masterpiece-2.2.33 |File Changes |Code Coverage
Code:
Quote:
#!/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
  #5 (permalink)  
Old 10-06-2007
okdev's Avatar
okdev okdev is offline
Registered User
  
 

Join Date: Sep 2007
Location: Dallas, Texas
Posts: 5
People Kindly Help

People Please Help me out.
Thanks
  #6 (permalink)  
Old 10-06-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
Hi.

Use double quotes in this situation -- single quotes do not allow variable expansion:

Code:
sed 's/$f3/$LABLE/' # Lable Replaced

I suggest you look over some tutorials like BASH Programming - Introduction HOW-TO ... cheers, drl
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0