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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to search a particular String form a file a write to another file using perl script
# 1  
Old 09-20-2013
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

Code:
Jan 11 04:05:10 linux100 |NOTICE |NET|NETDPCclient|thread:1|pid:[1234]: Message id, step 4 (abcdeff0-1g6g-91g3-1z2z-2mm605m90000)

Itried like this but its not working.

Code:
$Log="msg.log";
$OVOMSGIDFILE ="file.log";
 
open(LIST1, "<$Log") or die "Could not open chr1_22.txt: $!";
open(OUTPUT, "+>FILE") or die "Could not open chr1_23.txt: $!" ;
foreach my $line1 (<LIST1>)
{
chomp($line1);
# $line =~ m/'$1 if /Message id.+?(\w+-\w+-\w+-\w+-\w+)/'/);
'say $1 if /Message id.+?\((.+?)\)/' > OUTPUT;


Last edited by Scrutinizer; 09-20-2013 at 05:08 PM.. Reason: code tags
# 2  
Old 09-20-2013
Code:
#!/usr/bin/perl
#

use strict;

# define files
my $infile = "chr1_22.txt";
my $outfile = "chr1_23.txt";

# open infile in read mode and outfile in write mode
open(INFILE, "<$infile") or die "Unable to open: $infile.\n";
open(OUTFILE, ">$outfile") or die "Unable to write to: $outfile.\n";

# loop through infile and write captured regex pattern
# to outfile
while(<INFILE>) {
    print OUTFILE "$1\n" if(/.*Message\sid.*\((.*)\)/);
}

# close both files
close(INFILE);
close(OUTFILE);

# done
exit(0);

cat chr1_23.txt
abcdeff0-1g6g-91g3-1z2z-2mm605m90000

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 09-20-2013
Thank You So much for the timely reply.. sincere Thanks

Thank you :-)

Last edited by Raysf; 09-24-2013 at 01:04 PM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Search a string in a file in shell script?

I have a text file which is generated when the batch job is run. This batch may take few mins to run. When completed, the last line of the text file would be process completed. I need a shell script which will wait for this file till the process completed is printed in it, once found, it would move... (2 Replies)
Discussion started by: Lalat
2 Replies

2. Shell Programming and Scripting

Shell script to search all files for every string in another file

Hello All I have a pattern.txt file in source directory ((/project/source/) in linux server and data looks like: 123abc17 234cdf19 235ifg20 I have multiple log files in log directory (/project/log/) in linux server and data for one log file looks like: <?xml version="1.0" processid... (11 Replies)
Discussion started by: pred55
11 Replies

3. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

6. Programming

help need in the perl script that create one xml file form multiple files.

Hi every one, Please excuse me if any grammatical mistakes is there. I have multiple xml files in one directory, I need to create multiple XML files into one XML file.example files like this</p> file1:bvr.xml ... (0 Replies)
Discussion started by: veerubiji
0 Replies

7. Shell Programming and Scripting

search 3 file and write to 4th file (a bit complex)

hi buddies; rollbackip.txt:10.14.3.65 2 10.14.3.65 3 ... lookup.txt: ... 10.14.3.65 2 10.14.5.55 1 55 10.14.6.66 1 66 10.14.3.65 3 10.14.7.77 3 77 10.14.8.88 2 88 10.14.9.99 4 99 ... ip-port.txt ... port111 3 10.14.5.55 57 port111 2 10.14.5.55 51 port111 1 10.14.5.55 59 ->... (7 Replies)
Discussion started by: gc_sw
7 Replies

8. Shell Programming and Scripting

write shell script to search file in folder

hi .. i have a problem how to search file with date and version number(ms_2.0_dd/mm/yy_24)in folder.Here 24 is version number and compare the this file to other file which is in another folder and if does not match then copy this file to respective folder.Also copy different files in different... (1 Reply)
Discussion started by: shubhig15
1 Replies

9. Shell Programming and Scripting

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: 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... (3 Replies)
Discussion started by: LuckyGuy
3 Replies

10. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies
Login or Register to Ask a Question