The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
parsing a string in a shell script asutoshch Shell Programming and Scripting 19 05-26-2008 09:18 PM
shell script parsing with sed jjamd64 UNIX for Dummies Questions & Answers 5 12-11-2007 04:51 PM
Parsing a line in Shell Script unishiva Shell Programming and Scripting 3 11-01-2007 04:30 PM
Help in parsing a CSV file with Shell script mihirk Shell Programming and Scripting 10 06-24-2007 10:58 AM
Parsing a file in Shell Script sendhilmani123 Shell Programming and Scripting 4 11-30-2006 02:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-08-2007
gurpreet470 gurpreet470 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 4
Shell script for parsing 300mb log file..

am relatively new to Shell scripting.
I have written a script for parsing a big file. The logic is:
Apart from lot of other useless stuffs, there are many occurances of <abc> and corresponding </abc> tags. (All of them are properly closed)
My requirement is to find a particular tag (say <data>1234</data>) enclosed anywhere between <abc> </abc> tags.
If found, i have to store 4th line below the <abc> tag in a temp file.

A typical log file looks like:

************************
<pqr>
......
some data
some other data
.........
</pqr>
some text data
...........
<abc>
blah
blah
.....
<id>12345</id>
blah...
......
<data>1234</data>
</abc>
........
.....
.....

<abc>
blah
blah
.....
<id>12345</id>
blah...
...
</abc>
..........
<rst>
...
...
</rst>
some text data...

****************************

OUtput of the script should be <id>12345</id> stored in some temp file.

THe script I am using is:

********************

rm -f temp.log
filename=$1
OK=0

while read line1
do


if [ "$line1" = "<abc>" ]; then
OK=1
fi

if [ "$OK" -eq 1 ]; then
echo $line1 >> temp_file
fi

if [ "$line1" = "</abc>" ]; then
OK=0
fi

if [ "$OK" -eq 0 ] ; then

if [ -f temp_file ]; then

while read line2

do

if [ "$line2" = "<data>1234</data>" ]; then

cat temp_file | awk '{ if ( NR == 4){print($0) } }' >> temp.log

fi

done < temp_file

rm temp_file

fi

fi

done < $filename.log

*******************************

The <abc></abc> tags come in the last portion generally (Not always), somewhere around after 500000 lines... and usually, file has around 700000 lines.

The script runs, and keeps running, and I do find 2 records which are in the initial lines stored in the temp file. But after some 6-7 minutes, script ends abruptly, saying,
scriptname.sh test: argument expected.

Can someone help me out on this?
  #2 (permalink)  
Old 02-08-2007
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
*GNU Awk* solution:

Code:
awk '/data>1234/{print $5}' RS="<abc>" FS="\n" infile
  #3 (permalink)  
Old 02-09-2007
gurpreet470 gurpreet470 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 4
Script not working..

Quote:
Originally Posted by radoulov
*GNU Awk* solution:

Code:
awk '/data>1234/{print $5}' RS="<abc>" FS="\n" infile
I am really impressed by the awk power..
After reading your response, read a few articles on awk.
Somehow, the script is still not working, and ending saying..
**********************
awk: record `/response>
Fri 00:27:...' too long
record number 22
**********************
I am having high hopes on you now

Thanks
  #4 (permalink)  
Old 02-09-2007
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
Try to make the records shorter. You said that the <abc></abc> tags come in the last portion around after 500000 line so the first record is really long . You can make it shorter including other tags/strings (that you have at the beginig of the file) in the RS: for example RS="<abc>|<other_tag>". Or, if you prefer, you can send me a zipped copy of your file privately, so I could give a try
BTW, what's your Awk version?
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0