![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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? |
![]() |
| Bookmarks |
| Tags |
| unix commands |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|