Parse file contents in perl...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse file contents in perl...
# 1  
Old 07-06-2010
Parse file contents in perl...

Hi,

I have the file like this:

Code:
#Contents of file 1 are:

Dec 10 12:33:44 User1 Interface: Probe

Dec 10 12:33:47 uSER1 SOME DATA

Dec 10 12:33:47 user1 Interface: MSGETYPE

Dec 10 12:34:48 user1 ID: 10.

Dec 10 12:33:55 user1 Interface: MSGTYPE

Dec 10 12:33:55 user1 Id: 9

Dec 10 13:09:44 user1 Method: Push down

Dec 10 13:16:15 User1 Idlist: 12:10:102:100

I should parse this file and retrieve only MSGETYPE. The output is as shown below.

Code:
Dec 10 12:33:55 user1 Interface: MSGTYPE

Dec 10 12:33:55 user1 Id: 9

Dec 10 13:09:44 user1 Method: Push down

Dec 10 13:16:15 User1 Idlist: 12:10:102:100

The file will contain many MSGTYPE but it should parse only the above output lines.

How can i get the above output?

Regards
# 2  
Old 07-06-2010
Hi,Try this

Code:
#!/usr/bin/perl
undef $/;
while (<>) {
chomp;
if ( /.*\:\sMSGTYPE\n\n.*\n\n.*\n\n.*/) { print $&,"\n"; }
}

Code:
perl perlscript inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse through a txt file PERL scripting

Below is a perl code I am trying. #!/usr/bin/perl #use strict; use warnings qw/ all FATAL /; use constant ENV_FILE => '/apps/env_data.txt'; $uenv = $ARGV; my $input = $uenv; open my $fh, '<', ENV_FILE or die sprintf qq{Unable to open "%s" for input: $!}, ENV_FILE; ... (2 Replies)
Discussion started by: Tuxidow
2 Replies

2. Shell Programming and Scripting

awk or perl to parse file

I have an input file attached that I am trying to parse in tab-delimanted format: The chromosomal variant column contains all the information: parse rules: 1. 4 zeros after the NC_ and the digits before the . 2. digits after the g. repeated twice separated by a tab 3. letter before the > 4.... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

Perl: Parse Hex file into fields

Hi, I want to split/parse certain bits of the hex data into another field. Example: Input data is Word1: 4f72abfd Output: Parse bits (5 to 0) into field word1data1=0x00cd=205 decimal Parse bits (7 to 6) into field word1data2=0x000c=12 decimal etc. Word2: efff3d02 Parse bits (13 to... (1 Reply)
Discussion started by: morrbie
1 Replies

4. Shell Programming and Scripting

Perl Help - Assigning variables to text file contents

I am looking to create a perl script which will take numbers from a simple text file, convert them from decimal to hex, and then rewrite those values in the file or create a new file with the hex numbers(whichever's easier). My text document for example would be something as simple as 1312... (6 Replies)
Discussion started by: samh785
6 Replies

5. Shell Programming and Scripting

Mail the contents of a file in perl

Hi, I'm trying to read the contents of a file (message.txt), put them in a mail and then mail it This is what I have thus far but I having trouble referencing the file. I'm trying to put it into an array so any ideas would be helpful ... $to='user.n@domain.com'; $from= 'username';... (7 Replies)
Discussion started by: cillmor
7 Replies

6. Shell Programming and Scripting

how to read the contents of a file using PERL

Hi My requirement is to read the contents of a fixed length file and validate the same. But am not able to read the contents of the file and when i tried it to print i get <blank> as an output... I used the below satatements for printing the contents ... (3 Replies)
Discussion started by: meva
3 Replies

7. Shell Programming and Scripting

how to parse contents in a column in a file

hi all, i am facing this problem ,please help. suppose a file contains conetnts as below: john itemspurchased/date john itempurchased/date james itempurchased/date john items purchsed/date so here i want to store john only once in a variable but retreive his 3... (5 Replies)
Discussion started by: saapa
5 Replies

8. Shell Programming and Scripting

Looking for a perl script (windows) to delete the contents of a file

Hi All, Im having a file named logserver.txt. I want a perl script to take a backup of that file, along with the datestamp; move the file to a different location or empty the contents of the file after backup. Remember, the file gets generated when the related service starts. My condition is... (14 Replies)
Discussion started by: ntgobinath
14 Replies

9. Shell Programming and Scripting

Unix shell script to parse the contents of comma-separated file

Dear All, I have a comma-separated file. 1. The first line of the file(header) should have 4 commas(5 fields). 2. The last line of the file should have 1 comma(2 fields). Pls help me in checking this condition in a shell script. And the number of lines between the first line and last... (11 Replies)
Discussion started by: KrishnaSaran
11 Replies

10. Shell Programming and Scripting

CSV File parse help in Perl

Folks, I have a bit of an issue trying to obtain some data from a csv file using PERL. I can sort the file and remove any duplicates leaving only 4 or 5 rows containing data. My problem is that the data contained in the original file contains a lot more columns and when I try ro run this script... (13 Replies)
Discussion started by: lodey
13 Replies
Login or Register to Ask a Question