Sponsored Content
Top Forums Shell Programming and Scripting Extract string from a file & write to a new file (Perl) Post 302372491 by LuckyGuy on Wednesday 18th of November 2009 06:03:07 AM
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!
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Locale::Maketext::Extract::Plugin::Perl(3)		User Contributed Perl Documentation		Locale::Maketext::Extract::Plugin::Perl(3)

NAME
Locale::Maketext::Extract::Plugin::Perl - Perl format parser VERSION
version 0.99 SYNOPSIS
$plugin = Locale::Maketext::Extract::Plugin::Perl->new( $lexicon # A Locale::Maketext::Extract object @file_types # Optionally specify a list of recognised file types ) $plugin->extract($filename,$filecontents); DESCRIPTION
Extracts strings to localise (including HEREDOCS and concatenated strings) from Perl code. This Perl parser is very fast and very good, but not perfect - it does make mistakes. The PPI parser (Locale::Maketext::Extract::Plugin::PPI) is more accurate, but a lot slower, and so is not enabled by default. SHORT PLUGIN NAME
perl VALID FORMATS
Valid localization function names are: translate maketext gettext l loc x _ __ KNOWN FILE TYPES
.pm .pl .cgi SEE ALSO
xgettext.pl for extracting translatable strings from common template systems and perl source files. Locale::Maketext::Lexicon Locale::Maketext::Extract::Plugin::Base Locale::Maketext::Extract::Plugin::PPI Locale::Maketext::Extract::Plugin::FormFu Locale::Maketext::Extract::Plugin::TT2 Locale::Maketext::Extract::Plugin::YAML Locale::Maketext::Extract::Plugin::Mason Locale::Maketext::Extract::Plugin::TextTemplate Locale::Maketext::Extract::Plugin::Generic AUTHORS
Audrey Tang <cpan@audreyt.org> COPYRIGHT
Copyright 2002-2013 by Audrey Tang <cpan@audreyt.org>. This software is released under the MIT license cited below. The "MIT" License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. AUTHORS
o Clinton Gormley <drtech@cpan.org> o Audrey Tang <cpan@audreyt.org> COPYRIGHT AND LICENSE
This software is Copyright (c) 2014 by Audrey Tang. This is free software, licensed under: The MIT (X11) License perl v5.18.2 2014-02-03 Locale::Maketext::Extract::Plugin::Perl(3)
All times are GMT -4. The time now is 08:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy