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

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Using while loop to read a file

Reply to Message Icon

Original Message
Name: roopa_arjunan
Date: August 17, 2005 at 04:58:53 Pacific
Subject: Using while loop to read a file
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


Report Offensive Message For Removal


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


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal


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


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal

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

hi ,
Thank you very much.

Roopa.A


Report Offensive Follow Up For Removal

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


Report Offensive Follow Up For Removal






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



Results for: Using while loop to read a file

Shell script to read a file and pro
    Summary: Hi, I am trying to read a file which has two fields i.e tab seperated. Each line has an OldImage Name and a NewImage Name. Can anyone help me to write a shell script which reads the file and changes t...
www.computing.net/answers/unix/shell-script-to-read-a-file-and-pro/4304.html

how to read a file
    Summary: Hi how to read .csv file in unix for ex: i have a file called list_chksum.csv how can i read it in unix? thanks in advance ...
www.computing.net/answers/unix/how-to-read-a-file/8202.html

Reading a file line by line
    Summary: Hi, I wrote the following code to read a file line by line #!/bin/ksh for i in `cat a.txt` do echo $i done This is not showing me the file contents line bye line. Instead I am seeing a word by word ou...
www.computing.net/answers/unix/reading-a-file-line-by-line/5633.html








Which MP3 player do you have?

iPod/iPhone
Zune
Something Else
None


View Results

Poll Finishes Today.
Discuss in The Lounge
Poll History






Data Recovery Software