Script to Extract the line from File with specified offset


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Script to Extract the line from File with specified offset
# 1  
Old 05-02-2006
Script to Extract the line from File with specified offset

Hi All,

I need to extract only XML details from large log file which may contain other unwanted junk details.

For example, our xml will be start as <OUTBOUND_MESSAGE .....> and ends with </OUTBOUND_MESSAGE>. I want to extract only lines between these start and end tag (Including these tags) from the big file (say about 50000 lines) and dump into another file or console output. Can you please provide help on this?

Thinakar
# 2  
Old 05-02-2006
Try this bit of perl code:
Code:
#!/opt/perl/bin/perl

$ARGC=@ARGV;
if($ARGC!=1) {
        die "usage: ./test.pl <filename>\n";
}
$flag=0;
die "cannot open $ARGV[0]!" unless open FILEHNDL,$ARGV[0];
while($line=<FILEHNDL>) {
        if($line=~/?OUTBOUND_MESSAGE>/) {
                if(!$flag) {
                        $flag=1;
                }
        }
        elsif($flag) {
                print $line;
        }
}


Last edited by blowtorch; 05-02-2006 at 03:49 PM.. Reason: small change in code
# 3  
Old 05-03-2006
Thanks for your reply. This code may not work because my XML format will vary after "<OUTBOUND_MESSAGE" as I have attached. When I check with egrep I get the following result. Now what I need is I need to read onlly the lines between 21047 - 21089, 22162 - 22201 and 22889 - 22926. I prefer Unix solution than perl as I don't have even basic knowledge in perl.

cnbas-clintgw-1a> egrep -in "<OUTBOUND_MESSAGE|</OUTBOUND_MESSAGE>" cramer2router_20060422131.log
21047:<OUTBOUND_MESSAGE xmlns="http://www.logica.com/eai/adapter/outbound/data/dbmc/CRA-SECTOR">
21089:</OUTBOUND_MESSAGE>
22162:<OUTBOUND_MESSAGE xmlns="http://www.logica.com/eai/adapter/outbound/data/dbmc/CRA-SWITC
22201:</OUTBOUND_MESSAGE>
22889:<OUTBOUND_MESSAGE xmlns="http://www.logica.com/eai/adapter/outbound/data/dbmc/CRA-CELL"
22926:</OUTBOUND_MESSAGE>

Thanks
# 4  
Old 05-04-2006
I tried the following code using a varying <OUTBOUND_MESSAGE ...> input and it worked. Can you try it?
Code:
#!/usr/bin/perl

$ARGC=@ARGV;
if($ARGC!=1) {
        die "usage: ./test.pl <filename>\n";
}
$flag=0;
die "cannot open $ARGV[0]!" unless open FILEHNDL,$ARGV[0];
while($line=<FILEHNDL>) {
        if($line=~/\/?OUTBOUND_MESSAGE/) {
                if(!$flag) {
                        $flag=1;
                }
        }
        elsif($flag) {
                print $line;
        }
}

# 5  
Old 05-04-2006
I ran a search on the site for for similar posts, and you have created another post for the same problem. This is a violation of rules. Please follow the rules and most importantly, have patience.
# 6  
Old 05-04-2006
I remember to use awk to separate lines between pair of words.
Try man awk for this. I found this in man awk.

Quote:
Print all lines between start/stop pairs:

/start/, /stop/
see if this helps you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Dummies Questions & Answers

File name offset

Dear all, I want to offset the file numbers. can you please make some awk code or linux code for the same. Example: input file names ANI_WFMASS_PIST00001.gif ANI_WFMASS_PIST00002.gif . . . ANI_WFMASS_PIST0000n.gif offset --> 30 ANI_WFMASS_PIST00031.gif ANI_WFMASS_PIST00032.gif... (14 Replies)
Discussion started by: kri321shna
14 Replies

3. UNIX for Advanced & Expert Users

Grep --byte-offset not returning the offset (Grep version 2.5.1)

Hi, I am trying to get the position of a repeated string in a line using grep -b -o "pattern" In my server I am using GNU grep version 2.14 and the code is working fine. However when I am deploying the same code in a different server which is using GNU grep version 2.5.1 the code is not... (3 Replies)
Discussion started by: Subhamoy
3 Replies

4. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

5. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 Replies

6. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

7. Shell Programming and Scripting

Script to extract line from logfile

Hi , Can someone help me,I am not well versed with scripting,it is very urjent. Need a script in perl or shell for the following task. The logfile contains several entries of which one particular string to be searched and that complete line has to be removed and copied to another file to... (25 Replies)
Discussion started by: garryt21@rediff
25 Replies

8. Shell Programming and Scripting

extract a line from a file using the line number

Hello, I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4). Extra Notes: -I am working in a bash script. -The... (7 Replies)
Discussion started by: grandtheftander
7 Replies

9. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies

10. UNIX for Dummies Questions & Answers

Reading a file from a specified offset

Hi, I want to read a file from a specified offset from the start of file. With the read command, is it possible to do so. Please suggest. Is there any other alternative? Thanks, Saurabh (2 Replies)
Discussion started by: saurabhsinha23
2 Replies
Login or Register to Ask a Question