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
loop problem paddock Shell Programming and Scripting 2 09-23-2008 11:15 AM
for loop problem mdap Shell Programming and Scripting 3 08-16-2008 02:27 PM
Problem in For Loop The Observer Shell Programming and Scripting 2 05-28-2008 03:43 AM
Problem with while loop and SQL nandajk UNIX for Dummies Questions & Answers 20 05-04-2007 07:19 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 11-16-2008
dawn_1030 dawn_1030 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
Double while loop problem

Hi all, i have encountered a strange problem with the double while loop. The aim of the two while loops is to compare 2 files, tmplist.txt is the reference file with the complete list of servers. Please see the attached picture for the logic and relationship between input and output.

The 2 while loops should search the servers in /test.csv in the /tmplist.txt, line by line. For example, search first item in /test.csv server2 in /tmplist.txt, starting from first line in /tmplist.txt (LINENUM=1), if not found then output a 'NA' and then go to next line in /tmplist.txt until it finds the matching server2, and update LINENUM at the same time. Else will proceed to search the next server server3 in /tmplist.txt from the current line onwards (since all servers are always listed in alphabatic order).

The variable LINENUM (indicator of line number) used in the inner while loop. It is predefined as 1, after the if condition if [ $SERVER = $REFS ] is true, LINENUM will increment and then break out the inner while loop and continue in the outer while loop. However, in the outer while loop, every time it loops and reads SERVER COUNT, the LINENUM starts from the initial value 1 again instead of from the incremented LINENUM coming from the inner while loop.


Below shows part of the program with the details of 2 while loops.
Could anyone help to explain to me what is wrong here? This is my first UNIX assignment, will really appreciate your help!!


#!/bin/bash
TYPE=P2PS
LINENUM=1

while read SERVER COUNT; do

cat tmplist.txt | while read LINE; do

REFS=$(head -$LINENUM tmplist.txt | tail -1)

if [ $SERVER = $REFS ]; then
let LINENUM++
echo -n ","$COUNT >>t.csv
break
else
let LINENUM++
echo -n ",NA" >> t.csv
continue
fi
done

done < test.csv
Attached Images
File Type: bmp untitled.bmp (177.0 KB, 13 views)
  #2 (permalink)  
Old 11-16-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,126
The inner loop is a sub-process. Anything changed there has no effect on the parent process. It's the same situation as

X=1
bash -c "X=2"
echo $X

Both of your loops are reading from stdin. Each process only has 1 stdin so your two loops absolutely need to be separate processes.
  #3 (permalink)  
Old 11-16-2008
creepy cripple creepy cripple is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
There is no need for 2 loops, you have to use powerful unix commands in your scripts, like grep. This should work:
Of course, t.csv must be empty before executing the script.

while read LINE
do
aux=$(grep $LINE test.csv)
if [[ ! -z $aux ]]
then
echo "$aux" >> t.csv
else
echo "NA" >> t.csv
fi
done <tmplist.txt
  #4 (permalink)  
Old 11-16-2008
elbrand elbrand is offline
Registered User
  
 

Join Date: Apr 2008
Location: Wolfenbuettel/Germany
Posts: 12
the read in the inner loop is a subprocess. All variable values set or changed inside the inner loop are not known outside of it. If the files are small try to exchange the while-construct against a for-loop. If not, store meta-informations in the inner loop into files to have them outside. But this can decrease the speed very much.
But actually I do not see the need for double loops. Why not grep each line from tmplist.txt on the first file?
  #5 (permalink)  
Old 11-16-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,322
A solution with awk:
Code:
awk '
NR==FNR{a[$1]=$2;next}
$0 in a{print $0,a[$0];next}
{print "NA"}
' test.csv tmplist.txt > t.csv
  #6 (permalink)  
Old 11-17-2008
dawn_1030 dawn_1030 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
Thank you so much for your replies! Really powerful commands!
My script works now. Thanks all!
Closed Thread

Bookmarks

Tags
unix commands

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 06:15 PM.


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