Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home
General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2
Drivers
Driver Scan
Driver Forum
Software
Automatic Updates
BIOS Updates
My Computing.Net
Solution Center
Free IT eBook
Howtos
Site Search
Message Find
RSS Feeds
Install Guides
Data Recovery
About
Home
Using while loop to read a file
Original Message
Name: roopa_arjunan
Date: August 17, 2005 at 04:58:53 Pacific
Subject: Using while loop to read a fileOS: unixCPU/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
Subject: Using while loop to read a file
Reply: (edit )hi I am sorry i have given quote wrongly in line no.8but 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
Subject: Using while loop to read a file
Reply: (edit )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 4
Name: Luke Chi
Date: August 17, 2005 at 06:27:07 Pacific
Subject: Using while loop to read a file
Reply: (edit )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
Subject: Using while loop to read a file
Reply: (edit )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
Subject: Using while loop to read a file
Reply: (edit ) ok i used this then also its not geting the value for NRtname=$(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
Subject: Using while loop to read a file
Reply: (edit )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
Subject: Using while loop to read a file
Reply: (edit )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
Subject: Using while loop to read a file
Reply: (edit )#!/bin/sh cat report.txt | while read LINE do FOURTH=`echo $LINE | awk -F"|" '{print $4}'` # put your sode here doneLuke Chi
Report Offensive Follow Up For Removal
Use following form to reply to current message: