Extract string from a file & write to a new file (Perl)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract string from a file & write to a new file (Perl)
# 1  
Old 11-18-2009
Extract string from a file & write to a new file (Perl)

Hi,

This is the first time playing around with perl and need some help.

Assuming if i have a line of text that looks like this:
Code:
Date/Time=Nov 18 17:12:11;Device Name=192.168.1.1;Device IP=192.168.1.1;Device Class=IDS;Source IP=155.212.212.111;Source Name=UNKNOWN;Source Port=1679 (darcorp-lm);Destination IP=155.212.212.237;Destination Name=UNKNOWN;Destination Port=445 ();Message ID=SMB_Empty_Password_Failed:02;Message Text=Sep  2 17:34:23 %ISS-3-SMB_Empty_Password_Failed: 4018262^^2009-09-02 13:25:22.000^^SMB_Empty_Password_Failed^^(null)^^(null)^^3^^192.168.1.1^^155.212.212.111^^1679^^155.212.212.237^^netbios-ssn^^tcp^^Proventia G-Series^^(null)^^(null)^^1^^n^^445^^5^^27^^(null)^^Audit^^B^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)^^(null);

The delimiter above is semi-colon.

Need some help with a perl script to extract certain information from the line above and write the extracted info to a new file and the format of the new file should look like this:
Code:
-> from Message Text field
Summary !1000000000!: Sep  2 17:34:23 %ISS-3-SMB_Empty_Password_Failed: 4018262^^2009-09-02 13:25:22.000^^SMB_Empty_Password_Failed^^(null)^^(null)^^3^^192.168.1.1^^155.212.212.111^^1679^^155.212.212.237^^netbios-ssn^^tcp^^Proventia G-Series^^(null)^^(null)^^1^^n^^445^^5^^27^^(null)^^Audit^^B^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)^^(null)
 
-> from Source IP field
Asset ID !210000000!: 155.212.212.111
 
The following text are fixed:
Summary !1000000000!: 
Asset ID !210000000!:

Hope some kind soul out there can help with a perl script to do it.

Thank you in advance!

---------- Post updated at 07:03 PM ---------- Previous update was at 05:41 PM ----------

This is what i had created:
Code:
#!/usr/bin/perl -w
# read "org.log"
open (FILE, 'Text_OutPut.txt') or die "$!";
open (NEWFILE, '> remedysource.txt') or die "$!";
while (<FILE>) {
# replace txt with correct values
s/View Name=/ACTION: /;
s/Correlation Message ID=/Summary !1000000000!: /;
s/Correlation ID=/Notes !1000000151!: /;
s/Source IP=/Asset ID !210000000!: /;
s/Current Severity=/Priority !1000000164!: /;
@fields = split /;/, $_;
print NEWFILE "$fields[0]\n";
print NEWFILE "$fields[4]\n";
print NEWFILE "$fields[3]\n";
print NEWFILE "$fields[2]\n";
print NEWFILE "$fields[1]\n";
}
close (FILE);
close (NEWFILE);

It accomplishes what i want but can someone advise is there a better way of doing it?

Thx!
# 2  
Old 11-18-2009
Here's one way.

Code:
#!/usr/bin/perl

use strict;

my @a_out;
my @a_str;
my $str;

open INFILE, "<file_parse.in"
  or die "can't open file: $!";

while(<INFILE>)
{
   chomp($_);
   @a_str = split(';', $_);

   foreach $str (@a_str)
   {

      if ($str =~ m/Message Text/ )
      {
         $str =~ s/Message Text=/Summary !1000000000!: /;
         unshift (@a_out, "$str");
      }
      elsif ($str =~ m/Source IP/ )
      {
         $str =~ s/Source IP=/Asset ID !210000000!: /;
         unshift (@a_out, "$str");
      }

   }
}

close INFILE
  or die "can't close file: $!";

open OUTFILE, ">file_parse.out"
  or die "can't open file: $!";

foreach $str (@a_out)
{
   print (OUTFILE "$str\n");
}

close OUTFILE
  or die "can't close file: $!";

output
Code:
Summary !1000000000!: Sep 2 17:34:23 %ISS-3-SMB_Empty_Password_Failed: 4018262^^2009-09-02 13:25:22.000^^SMB_Empty_Password_Failed^^(null)^^(null)^^3^^192.168.1.1^^155.212.212.111^^1679^^155. 212.212.237^^netbios-ssn^^tcp^^Proventia G-Series^^(null)^^(null)^^1^^n^^445^^5^^27^^(null)^^Audit^^B^^(null)^^(null)^^(null)^^(null)^^(null)^^ (null)^^(null)^^(null)^^(null)^^(null)
Asset ID !210000000!: 155.212.212.111


Last edited by jsmithstl; 11-18-2009 at 08:10 AM..
# 3  
Old 11-18-2009
removed my post

removed my post

Last edited by gaya; 11-18-2009 at 10:49 AM..
# 4  
Old 11-18-2009
Gaya,

If this is a new request for help, please remove your post, if possible, and open a new thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

2. Shell Programming and Scripting

Extract text from file then write variable

I have a text file that has many lines, and for each line I need to extract different sections of text, then write several variables from the data. I can split out the various sections by selecting their position on each line as the column sizes will never vary. A sample of my text file is below... (2 Replies)
Discussion started by: kcpoole
2 Replies

3. Shell Programming and Scripting

Extract data from XML file and write in CSV file

Hi friend i have input as following XML file <?xml version="1.0"?> <Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.02"> <BkToCstmrDbtCdtNtfctn> <GrpHdr><MsgId>LBP-RDJ-TE000000-130042430010001001</MsgId><CreDtTm>2013-01-04T03:21:30</CreDtTm></GrpHdr>... (3 Replies)
Discussion started by: mohan sharma
3 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. Shell Programming and Scripting

Perl extract number from file & write to file

I have 1 file that has elements as follows. Also the CVR(10) and the word "SAUCE" only appear once in the file so maybe a grep command would work? file1 CVR( 9) = 0.385E+05, ! VEHICLE CVR(10) = 0.246E+05, ! SAUCE CVR(11) = 0.162E+03, ! VEHICLE I need to extract the... (6 Replies)
Discussion started by: austinj
6 Replies

6. Shell Programming and Scripting

How to write a script to extract strings from a file.

Hello fourm members, I want to write a script to extarct paticular strings from the all type of files(.sh files,logfiles,txtfiles) and redirect into a log file. example: I have to find the line below in the script and extract the uname and Pwds. sqsh -scia2007 -DD0011uw01 -uciadev... (5 Replies)
Discussion started by: rajkumar_g
5 Replies

7. Shell Programming and Scripting

extract string and sending it into a new file in perl program

Hi, I have a input file with following values (test.out) I would like to grep all lines with word 'PANIC' and sent it another file using perl program with grep command. I have been trying different ways and not working. Pls advice. Thanks a lot for the help. --example--... (3 Replies)
Discussion started by: hudson03051nh
3 Replies

8. Shell Programming and Scripting

Extract data from an XML file & write into a CSV file

Hi All, I am having an XML tag like: <detail sim_ser_no_1="898407109001000090" imsi_1="452070001000090"> <security>ADM1=????</security> <security>PIN1=????</security> <security>PIN2=????</security> ... (2 Replies)
Discussion started by: ss_ss
2 Replies

9. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

10. Shell Programming and Scripting

How to delete a string pattern in a file and write back to the same file

I have a control file which looks like this LOAD DATA INFILE '/array/data/data_Finished_T5_col_change/home/oracle/emp.dat' PRESERVE BLANKS INTO TABLE SCOTT.EMP FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (................. ..................) How can i edit the... (1 Reply)
Discussion started by: mwrg
1 Replies
Login or Register to Ask a Question