Computing.Net > Forums > Unix > Using while loop to read a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Using while loop to read a file

Reply to Message Icon

Name: roopa_arjunan
Date: August 17, 2005 at 04:58:53 Pacific
OS: unix
CPU/Ram: unknown
Comment:

Hi,
I want to get the second field from a file (report.txt)like this:

$ cat report.txt
SheetName|RecordName|FieldName|TableName|ColumnName|Change
Summary||2||No specification received for output file record|MODIFIED
Central Checks|R3002SPLDXVOLUME|F3002-VOL1-VOL-SRL-NUM|SUBMISSION|SERIAL_NUM|DEL
ETED
Suspense data|RC3310SUSPMIGXDATA||||ADDED
Suspense data|RC3310SUSPMIGXDATA|FC3310-RECORD-TYPE|||ADDED

I am using while loop to get the fourth field.but it is giving error like this:

5
Summary||2||No specification received for output file record|MODIFIED
./example.sh: syntax error at line 12: `end of file' unexpected

I am getting the line count and using the line count in while loop how to go through the file and get the fourth field from the file.

My code is as follows:

#!/bin/sh
nooflines=`wc -l report.txt| sed -e "s/[a-zA-Z. ]*//g" `
echo $nooflines
i=2
echo `awk -F"|" 'NR==2 {print $4}' report.txt`
while [ $i -le $nooflines ]
do
echo `awk -F"|" `NR==$i {print $4}' report.txt`
echo $i
i=`expr $i + 1`
done


Thank you.

Roopa.A



Sponsored Link
Ads by Google

Response Number 1
Name: roopa_arjunan
Date: August 17, 2005 at 05:54:09 Pacific
Reply:

hi
I am sorry i have given quote wrongly in line no.8

but still its not working ,i.e , its not assigning the value of i to NR.
Please tell me how to assign

Roopa.A


0

Response Number 2
Name: Luke Chi
Date: August 17, 2005 at 06:10:42 Pacific
Reply:

Replace:

echo `awk -F"|" 'NR==$i {print $4}' report.txt`

with:

echo -v VAR_IN="$i" `awk -F"|" 'NR==VAR_IN {print $4}' report.txt`

Luke Chi


0

Response Number 3
Name: roopa_arjunan
Date: August 17, 2005 at 06:22:42 Pacific
Reply:

I tried but it is giving the following error

./example.sh[9]: -v: not found

Roopa.A


0

Response Number 4
Name: Luke Chi
Date: August 17, 2005 at 06:27:07 Pacific
Reply:

I'm sorry that I there was a typo there.

Use the following:

#!/bin/sh
nooflines=`wc -l report.txt| sed -e "s/[a-zA-Z. ]*//g" `
echo $nooflines
i=2
echo `awk -F"|" 'NR==2 {print $4}' report.txt`
while [ $i -le $nooflines ]
do
echo `awk -v VAR_IN="$i" -F"|" 'NR==VAR_IN {print $4}' report.txt`
echo $i
i=`expr $i + 1`
done

Luke Chi


0

Response Number 5
Name: roopa_arjunan
Date: August 17, 2005 at 06:30:11 Pacific
Reply:

I am using like this:

tname=$(echo -v I="$i" | awk -F"|" 'NR==I && length($4)>0 {print $4}' report.txt
)


It is assigning the value to I but it not assigning to NR
its displaying as NR==I only

Roopa.A


0

Related Posts

See More



Response Number 6
Name: roopa_arjunan
Date: August 17, 2005 at 06:36:18 Pacific
Reply:


ok i used this
then also its not geting the value for NR

tname=$(awk -v I="$i" -F"|" 'NR==I && length($4)>0 {print $4}' report.txt)

Roopa.A


0

Response Number 7
Name: Luke Chi
Date: August 17, 2005 at 06:46:14 Pacific
Reply:

1. I just checked your program. You wanted "to get the fourth field".

Replace whole program with:

awk -F"|" '{ print $4 }' report.txt

2. NR is awk reserved variable. You're not supposed to assign the value to it.


Luke Chi


0

Response Number 8
Name: roopa_arjunan
Date: August 17, 2005 at 06:51:45 Pacific
Reply:

hi thanks for ur overwhelming response.
but wat i want is i have to get fourth field one by one and do some manipulation so i have to go through the file record by record.
and see whether it has any string or not.the depending on that i will search for the string in a folder.
So i need separately every fourth field and store in a variable.

Roopa.A


0

Response Number 9
Name: Luke Chi
Date: August 17, 2005 at 07:01:36 Pacific
Reply:

#!/bin/sh

cat report.txt | while read LINE
do
FOURTH=`echo $LINE | awk -F"|" '{print $4}'`
# put your sode here
done


Luke Chi


0

Response Number 10
Name: roopa_arjunan
Date: August 17, 2005 at 23:16:46 Pacific
Reply:

hi ,
Thank you very much.

Roopa.A


0

Response Number 11
Name: roopa_arjunan
Date: August 18, 2005 at 00:50:12 Pacific
Reply:

hi Luke,
Can u tell me how to read from secon line onwards in that report.txt file.

Thank you.

Roopa.A


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Using while loop to read a file

Shell script to read a file and pro www.computing.net/answers/unix/shell-script-to-read-a-file-and-pro/4304.html

how to read a file www.computing.net/answers/unix/how-to-read-a-file/8202.html

Reading a file line by line www.computing.net/answers/unix/reading-a-file-line-by-line/5633.html