Print only matched pattern in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print only matched pattern in perl
# 1  
Old 01-24-2013
Power Print only matched pattern in perl

Hi,

I have script like below:

Code:
#!/usr/local/bin/perl

use strict;
use warnings;

while (<DATA>) {

  ( my ($s_id) = /^\d+\|(\d+?)\|/ ) ;

  if ( $s_id == 1 ){

    s/^(.*\|)*.*ABC\.pi=([\d.]+|[\w.]+)*.*ABC\.id=(\d+|[\w.]+).*$/$1$2|$3/s;

    print "$1$2|$3\n";

    }
  }


__DATA__
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.66~ABC.id=789137136770
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.67~ABC.id=789134713670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.68~ABC.id=789137213670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.69~ABC.id=78913713670
123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id=78913713670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id=78913713670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id=789137135670
123|1|456464|645646|4546|654~abc~dhghga~ABC.pi=112.33.44.55.70~ABC.id=789137153670
123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713670
123|1|456464|645646|4546|654~abc~dhghga~121322~456466874~8796896
123|2|456464|645646|4546|654~abc~dhghga~121322~456466874~6788708
123|2|456464|645646|4546|654~abc~dhghga~121322~456466874~6806

When I am executing I am getting output as follows:
Code:
123|1|456464|645646|4546|112.33.44.55.66|789137136770
123|1|456464|645646|4546|112.33.44.55.67|789134713670
123|1|456464|645646|4546|112.33.44.55.68|789137213670
123|1|456464|645646|4546|112.33.44.55.69|78913713670
Use of uninitialized value $2 in concatenation (.) or string at split_test.pl line 14, <DATA> line 5.
Use of uninitialized value $3 in concatenation (.) or string at split_test.pl line 14, <DATA> line 5.
1|
123|1|456464|645646|4546|112.33.44.55.70|78913713670
123|1|456464|645646|4546|112.33.44.55.70|78913713670
123|1|456464|645646|4546|112.33.44.55.70|789137135670
123|1|456464|645646|4546|112.33.44.55.70|789137153670

I am looking to get rid off the error. How can I do it?
# 2  
Old 01-24-2013
try the below
Code:
 
print "$1$2|$3\n";

to
 
if ($1 && $2 && $3) {print "$1$2|$3\n";}

# 3  
Old 01-24-2013
You are getting that error because, on line 5
Code:
123|1|456464|645646|4546|654~abc~dhghga~12.33.44.55.70~3713670

there is no matching pattern. itkamaraj gave a good solution. May be you could parse only lines with the pattern ABC.??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find matched pattern and print all based on certain conditions

Hi, I am trying to extract data based on certain conditions. My sample input file as below:- lnc-2:1 OnePiece tra_law 500 688 1 . . g_id "R792.8417"# tra_law_id "R792.8417.1"# g_line "2.711647"# KM "8.723820"# lnc-2:1 OnePiece room 500 510 1 . . g_id "R792.8417"# tra_law_id "R792.8417.1"#... (7 Replies)
Discussion started by: bunny_merah19
7 Replies

2. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

3. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

4. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

5. Shell Programming and Scripting

print the whole row in awk based on matched pattern

Hi, I need some help on how to print the whole data for unmatched pattern. i have 2 different files that need to be checked and print out the unmatched patterns into a new file. My sample data as follows:- File1.txt Id Num Activity Class Type 309 1.1 ... (5 Replies)
Discussion started by: redse171
5 Replies

6. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

7. Shell Programming and Scripting

How to print lines till till a pattern is matched in loop

Dear All I have a file like this 112534554 446538656 444695656 225696966 226569744 228787874 113536566 443533535 222564552 115464656 225445345 225533234 I want to cut the file into different parts where the first two columns are '11' . The first two columns will be either... (3 Replies)
Discussion started by: anoopvraj
3 Replies

8. Shell Programming and Scripting

print last matched pattern using perl

Hi, If there exist multiple pattern in a file, how can I find the last record matching the pattern through perl. The below script searches for the pattern everywhere in an input file. #! /usr/bin/perl -s -wnl BEGIN { $pattern or warn"Usage: $0 -pattern='RE' \n" and exit 255;... (5 Replies)
Discussion started by: er_ashu
5 Replies

9. Shell Programming and Scripting

SED: delete and print the only exact matched pattern

I am really need help with the regular expression in SED. From input file, I need to extract lines that have the port number (sport or dport) as defined. The input file is something like this time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23 bytes=2472 proto=6 sport=80 dport=1447... (6 Replies)
Discussion started by: new_buddy
6 Replies

10. Shell Programming and Scripting

HELP! PERL script to find matched pattern

Hi all, I just learnt Perl and I encountered a problem in my current project. For a verilog file, i am required to write a PERL script that could match pattern to output nitrolink and nitropack. I wont know what name to grep except the pattern below. the verilog file: nitrolink nitrolink... (1 Reply)
Discussion started by: kimhuat
1 Replies
Login or Register to Ask a Question