While loop read line Issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop read line Issue
# 1  
Old 04-22-2010
Error 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 the problem starts in the data set to make sure there are no special characters that are causing the issue.

Please let me know if this is a script issue or some size limit issue.

Code:
while read line
do
count=0
echo $line | cut -c250-310  | cut -d"_" -f3 | cut -c1-9 | sed 's/ //g' | read tran_id
echo $line | awk '{ n += gsub( "<TCRMContractPartyRoleBObj>", "") } END { print n }' | sed 's/ //g' | read count
count=$n
echo $tran_id"|"$count >> $XML_OUTPUT
done < $XML_INPUT_FILE

Output - Problem starts at line 386501 below and continues till end of file
Code:
+386498 TK1251683|1
+386499 TK1251684|2
+386500 TK1251684|4
+386501 nNameBObj|1
+386502 hodBObj><|1
+386503 e><XSpoke|1
+386504 tractType|1
+386505 ourceIden|1
+386506 ountryTyp|1

Thanks
Saurabh

Last edited by Scott; 04-22-2010 at 04:10 PM.. Reason: Please use code tags
# 2  
Old 04-22-2010
I see so many pipes in your code, and many are useless.

Provide some sample inputs for us to thin your code.
# 3  
Old 04-23-2010
First, i would quote $line in lines 4 and 5
Code:
echo "$line" | cut -c250-310  | cut -d"_" -f3 | cut -c1-9 | sed 's/ //g' | read tran_id
echo "$line" | awk '{ n += gsub( "<TCRMContractPartyRoleBObj>", "") } END { print n }' | sed 's/ //g' | read count

But sure there are better ways, as wrote rdcwayx above...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Performance issue - to read line by line

All- We have a performance issue in reading a file line by line. Please find attached scripts for the same. Currently it is taking some 45 min to parse "512444" lines. Could you please have a look at it and provide any suggestions to improve the performance. Thanks, Balu ... (12 Replies)
Discussion started by: balu1729
12 Replies

2. UNIX for Beginners Questions & Answers

Performance issue to read line by line

Hi All- we have performance issue in unix to read line by line. I am looking at processing all the records. description: Our script will read data from a flat file, it will pickup first four character and based on the value it will set up variables accordingly and appended the final output to... (11 Replies)
Discussion started by: balu1729
11 Replies

3. Shell Programming and Scripting

While read line loop

Based on text file: PATH:/media/hdd/Media/Video/title1 FILE:/media/cache/281662-14.jpg PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg PATH:/media/hdd/Media/Video/title4 FILE:/media/cache/281662-17.jpg... (12 Replies)
Discussion started by: TiedCone
12 Replies

4. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

5. Shell Programming and Scripting

IF awk in a while read line-loop

Hi As a newbe in scripting, i struggle hard with my first script. What i want to do is, bringing data of two files together. file1: .... 05/14/12-04:00:00 41253 4259 5135 5604 5812 5372 05/14/12-04:10:00 53408 5501 6592 7402 7354 6639 05/14/12-04:20:00 58748 6037 7292 8223... (13 Replies)
Discussion started by: IMPe
13 Replies

6. 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

7. Shell Programming and Scripting

While loop read line not working

Hi, I am trying to read a file line by line inside of a while loop. This while loop is part of a here document. while read line do ssh -t $2@$remotehost <<REMOTE ls path/to/dir > $path_to_dir while read line1 do echo "LINE --- $line" done... (4 Replies)
Discussion started by: mnanavati
4 Replies

8. Shell Programming and Scripting

While read line loop

Hi I'm writing a bash script which will read an input file and look for occurrences of the current user ($USER) executing the script. When i find the occurrence of the username I take that line and append it to a file with a line number and bracket display next to line. The input file has been... (12 Replies)
Discussion started by: BundBash
12 Replies

9. Shell Programming and Scripting

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

10. Shell Programming and Scripting

Nested while read line loop

Hi, Can anyone please help me: i'm trying to read a file with directory-names , then go to that directory and read another (output) file to perform some tasks per line (second read line in the part of script below). The problem is that after the nested while loop has finished, the first while... (7 Replies)
Discussion started by: Rakker
7 Replies
Login or Register to Ask a Question