While read issue...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers While read issue...
# 1  
Old 03-07-2008
While read issue...

I think I saw another post where someone said he an issue with a 'while within a while' but there were no solutions on it.

I have an input file of data columns separated by pipes "|" and I want to set each column to a variable and do something with it. This I can do.

When I get to a certain row of a certain value, I want to take a subset (i.e. grep for a certain key) of all rows from the same input file and do another while statement, but for some reason or other, the second while only runs once and it breaks back out to first while. This is just pseudo code:

while read a b c ; do
a=s/thing
b=s/thing
c=s/thing
if a=somethingspecial then
grep "somethingiwant" myinputfile > anewfile
while read a b c; do
a1=s/thing
b1=s/thing
c1=s/thing
do something as many times as grep found the key
done <anewfile
fi
done < myinputfile

The problem is that the inner while runs once even though I may have say, 20 rows to process. Any thoughts?

Thank you.
Gianni
# 2  
Old 03-07-2008
This is crazy. I use double loops just like that all the time. They work fine. Real code example....
Code:
$ cat doubleloop
#! /usr/bin/ksh

while read count ; do
        echo count = $count >temp
        yes some stuff | sed ${count}q >> temp
        while read line ; do
                echo this came from temp file: $line
        done < temp
done < countfile
exit 0
$
$
$
$
$ cat countfile
4
6
2
7
2
$
$
$
$
$
$ ./doubleloop
this came from temp file: count = 4
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: count = 6
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: count = 2
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: count = 7
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: some stuff
this came from temp file: count = 2
this came from temp file: some stuff
this came from temp file: some stuff
$

# 3  
Old 03-08-2008
It does sound crazy and not logical but that's what I experienced.

If I were to build on your example, what happens if you got to count '2' in your first while and you want to pull all '2's from your input file "countfile" (i.e grep '2' into another temp file) and process that temp file in the inner while?

I noticed in my situation that the inner while will pull only the second, third, fourth, etc. occurrence of '2' from countfile...and when it processes the temp file, it will do it once and go break out. This is on aix with ksh.

My work around was to duplicate the countfile into say countfile2 and grep for all '2' from countfile2 inside the inner while, then it processed all the records as expected and returned to the next record of the countfile.
In this example, '2' is simplistic but in my example, it's a table column name. I'm grepping for all records with this column so I can do some more processing with these columns. In my case, I could duplicate the file and it's small enough, but if it's a large file, it wouldn't be ideal to have two identical files just to handle a while issue...

I admit it's weird and not logical but I know I've encountered it before a few times in the past. My solution was always to switch to a 'for loop' with inner while. However, this time, I need the while loop in a while since I have way more variables to deal with then just a 'for x in... ' loop.

Gianni
# 4  
Old 03-08-2008
Quote:
Originally Posted by giannicello
I think I saw another post where someone said he an issue with a 'while within a while' but there were no solutions on it.

I have an input file of data columns separated by pipes "|" and I want to set each column to a variable and do something with it. This I can do.

When I get to a certain row of a certain value, I want to take a subset (i.e. grep for a certain key) of all rows from the same input file and do another while statement, but for some reason or other, the second while only runs once and it breaks back out to first while. This is just pseudo code:

while read a b c ; do
a=s/thing
b=s/thing
c=s/thing
if a=somethingspecial then
grep "somethingiwant" myinputfile > anewfile
while read a b c; do
a1=s/thing
b1=s/thing
c1=s/thing
do something as many times as grep found the key
done <anewfile
fi
done < myinputfile

The problem is that the inner while runs once even though I may have say, 20 rows to process. Any thoughts?

Thank you.
Gianni
see the red fonts. they are the same. try naming them differently
# 5  
Old 03-08-2008
That was a typo on my part in the example. However, the read a1 b1 c1 didn't resolve the issue. I do have it correct in my code (granted I called it s/thing totally different).

For the grep issue also, I'm wondering if the input file, when opened by a "while read", is in memory and when you do a grep of that file that has been read to a certain point, there's still a pointer in memory to that input file so then grep only sees everything after that first occurrence?

The first occurrence is like getting to the first '2' in Perderabo's example (a 'key word' I'm interested in doing more with) and trying to pull all '2's from the input file (countfile) again but grep doesn't see the first '2' anymore in the inner while...it only pulls the 2nd, 3rd, 4th occurrence, etc. when I sent the output to tmpcountfile ( grep $x countfile > tmpcountfile .... while read a1; do s/thing done<tmpcountfile).

It's hard to explain but I tried simple tests and it confirmed my issue with inner while. I opened the inner while tmpcountfile and the first occurrence is not an output from the grep when done within the inner while.
...
Gianni
# 6  
Old 03-08-2008
Please post a real executable shell script that illustrates your problem.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While read pipe input issue

Hello, I have an ffmpeg bash script which is working nice and I need to do the same for other sources. To create new scripts and to deal with multiple bash files sounds not logical. It is a bit hard to manage for me.. I wondered if it was possible to make my input file as variable. Then I... (1 Reply)
Discussion started by: baris35
1 Replies

2. Shell Programming and Scripting

File read format issue in UNIX

hi all. my loop is getting failed eventhoug it is 1=1 but it is failure message. any help plz Output expected : echo "sucesss" code out=`cat bit.txt` if ]; then echo "sucess" else echo "Failure" (2 Replies)
Discussion started by: arun888
2 Replies

3. Shell Programming and Scripting

ksh while read issue

Hello, I have used a chunk of ksh script similar to this in many places without any issue: while : do print; read OPTION?"Enter a number (q to quit): " expr ${OPTION} + 1 >/dev/null 2>&1 CHECKVAL=$? if }" != ${OPTION} ]; then ... (2 Replies)
Discussion started by: port43
2 Replies

4. Shell Programming and Scripting

Read line, issue with leading - and {}'s

Heyas With my forum search term 'issue with leading dash' i found 2 closed threads which sadly didnt help me. Also me was to eager to add the script, that i didnt properly test, and just now figured this issue. So i have this code: if ] then while read line do line="${line/-/'\-'}"... (7 Replies)
Discussion started by: sea
7 Replies

5. Shell Programming and Scripting

Issue in using read keyword twice

Hi, I have a situation where i need to read line by line from a text pad and with each line , i need to take inputs from command line and do some process. Using below code i am not able to use 'read' keyword twice. Can any one please help cat > t.txt a d c > cat > t.ksh while read... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

6. Shell Programming and Scripting

while read issue

I'm using while read in a script to create a file but when I paste on the screen it echos out different than the data. The file is created correctly and the script does as it should. Just trying to resolve what's being show on the screen. while read FILES do echo... (3 Replies)
Discussion started by: toor13
3 Replies

7. Shell Programming and Scripting

while read LINE issue

Hi, This is the script and the error I am receiving Can anyone please suggest ? For the exmaple below assume we are using vg01 #!/bin/ksh echo "##### Max Mount Count Fixer #####" echo "Please insert Volume Group name to check" read VG lvs |grep $VG | awk {'print $1'} > /tmp/audit.log ... (2 Replies)
Discussion started by: galuzan
2 Replies

8. UNIX for Advanced & Expert Users

read() from ttyS1 issue while write() is Ok

Hi! I've got a problem with reading from serial port, when I run this code on Digi ConnectCore Wi-9c. But writing to serial port is Ok. By the way, when I'm running this code on "full" Linux it is working Ok - I can read and write to serial without mistakes. Where is a problem? uname -a:... (3 Replies)
Discussion started by: Japonomatj
3 Replies

9. Shell Programming and Scripting

Multi Line 'While Read' command issue when using sh -c

Hi, I'm trying to run the following command using sh -c ie sh -c "while read EachLine do rm -f $EachLine ; done < file_list.lst;" It doesn't seem to do anything. When I run this at the command line, it does remove the files contained in the list so i know the command works ie... (4 Replies)
Discussion started by: chrispward
4 Replies

10. Shell Programming and Scripting

While loop read line Issue

Hi I am using while loop, below, to read lines from a very large file, around 400,000 rows. The script works fine until around line 300k but then starts giving incorrect result. I have tried running the script with a smaller data set and it works fine. I made sure to include the line where... (2 Replies)
Discussion started by: saurabhkumar198
2 Replies
Login or Register to Ask a Question