Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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|||ADDEDI 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' unexpectedI 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

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 assignRoopa.A

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

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`
doneLuke Chi

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 onlyRoopa.A

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

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

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

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

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

![]() |
![]() |
![]() |

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