Convert awk line into perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert awk line into perl
# 1  
Old 03-15-2014
Convert awk line into perl

I have an awk statement in a ksh script that looks for a certain string then looks at each line after to find another match. The match could be the next line or second down and it works well.
Code:
nawk 'BEGIN {FS=RS;RS="!"} /interface loopback0/
{for(i=1;i<=NF; i++) if ($i ~ /ip address/){split($i,a, OFS "|/");print a[3]}}' <file>

I am writing a perl script that I need to capture the same kind of match, but I can not find out how to find the string then look at the next lines until the second match is found. I have put together this together but it only looks at the second line after the string is found, which is where my data is usually but sometimes it is on the first next line.
Code:
my $loopback = "interface loopback0 ";
 
foreach my $host (@HOST){
my $out_fh = "$CONFIG_DIR/$host";
open (FILE, "<$out_fh") or die "Can't open $out_fh";
my @lines = <FILE>;
my $lines; my $i;
for $i(0..$#lines) {
next unless $lines[$i] =~ /$loopback/;
my $a = $i + 2 < 0 ? 0 : $i + 2;;
for $i($a) {
if ($lines[$i] =~ /ip address/) {
my ($out) = (split / /, $lines[$i])[4];
$out = (split /\//, $out)[0];
print "$out\n";
}
}
}
}
close (FILE);

Can someone help me find something that works?

A file example of what the first string finds is like this:
Code:
!
interface loopback0 loopback
description My Router Loopback
ip address 10.30.160.10/32 tag 200
ip source-address tftp ftp 
!
interface loopback1 loopback
description External BGP
ip address 10.159.192.9/32 tag 201
!
interface loopback2 loopback
ip address 10.159.168.33/32 tag 202
ip source-address radius 
!
interface loopback3 loopback
description ID: Management;
ip address 10.81.114.6/32 tag 203
ip source-address snmp syslog 
!


Last edited by Scrutinizer; 03-15-2014 at 04:39 PM.. Reason: icode to code tags
# 2  
Old 03-15-2014
Hi.

Perhaps:
Code:
NAME
       a2p - Awk to Perl translator

SYNOPSIS
       a2p [options] [filename]

DESCRIPTION
       A2p takes an awk script specified on the command line (or from standard
       input) and produces a comparable perl script on the standard output.

See man a2p for more details ... cheers, drl
# 3  
Old 03-16-2014
As drl said you can use a2p here is example

Code:
$ echo nawk 'BEGIN {FS=RS;RS="!"} /interface loopback0/{for(i=1;i<=NF; i++) if ($i ~ /ip address/){split($i,a, OFS "|/");print a[3]}}' | a2p
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
			# this emulates #! processing on NIH machines.
			# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
			# process any FOO=bar switches

$[ = 1;			# set array base to 1
$FS = ' ';		# set field separator
$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator

$FS = $/;
$/ = '!';

whle (<>) {
     chomp;	# strip record separator
    @Fld = split($FS, $_, -1);
    print $_ if $nawk;if (/interface loopback0/) {
	for ($i = 1; $i <= $#Fld; $i++) {
	    if ($Fld[$i] =~ /ip address/) {
		@a = split($, . '|/', $Fld[$i], -1);
		print $a[3];
	    }
	}
    }
}

# 4  
Old 03-16-2014
Thank you for replying, but I was looking for a code that did not include awk. I am new to perl and know it is very rich, so I believe this can be done strictly in perl.
# 5  
Old 03-19-2014
I made some updates to the script and it works, may not very beautiful, but it does what I need to look at the first and second line after the string match.
Code:
foreach my $host (@HOST){
my $out_fh = "$CONFIG_DIR/$host";
open (FILE, "<$out_fh") or die "Can't open $out_fh";
     my @lines = <FILE>;
     my $lines; my $i;
     for $i(0..$#lines) {
        next unless $lines[$i] =~ /$loopback/;
           my $a = $i + 1 < 0 ? 0 : $i + 1;;
           my $b = $i + 2 < 0 ? 0 : $i + 2;;
             for $i($a) {
                if ($lines[$i] =~ /ip address/) {
                   my ($out) = (split / /, $lines[$i])[4];
                   $out = (split /\//, $out)[0];
                   print "$out\n";
                }
              }
             for $i($b) {
                if ($lines[$i] =~ /ip address/) {
                   my ($out) = (split / /, $lines[$i])[4];
                   $out = (split /\//, $out)[0];
                   print "$out\n";
                }
              }
      }
close (FILE);
}

# 6  
Old 03-19-2014
Quote:
Originally Posted by numele
Thank you for replying, but I was looking for a code that did not include awk. I am new to perl and know it is very rich, so I believe this can be done strictly in perl.
I think you misunderstood; a2p generates a perl script that does not depend on awk.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert rows into columns using awk or perl

hi friends, i am able to parse cvs diff file using bit of cut and grep commands to produce following output in text file '''cvs-diff.txt''' Package-Name = dev-freetype. Old-Version = 2.4.8 New-Version = 2.4.10 Patches-removed = freetype-2.4.8-cross-compile.patch... (2 Replies)
Discussion started by: alexzander18
2 Replies

2. Shell Programming and Scripting

awk or sed - Convert 2 lines to 1 line

Hi, Just trying to get to grips with sed and awk for some reporting for work and I need some assistance: I have a file that lists policy names on the first line and then on the second line whether the policy is active or not. Policy Name: Policy1 Active: yes Policy... (8 Replies)
Discussion started by: guinch
8 Replies

3. Shell Programming and Scripting

Help with convert awk script into perl

Input file (a list of input file name with *.txt extension): campus.com_icmp_ping_alive.txt data_local_cd_httpd.txt data_local_cd.txt new_local_cd_mysql.txt new_local_cd_nagios_content.txt Desired output file: data local_cd_httpd data local_cd new local_cd_mysql new ... (9 Replies)
Discussion started by: perl_beginner
9 Replies

4. Shell Programming and Scripting

Convert awk command to Perl

Hi all; I have a Perl script that uses this awk command wrapped in a "system" call; it works exactly as I want it; however I want to keep this a Perl script and have very few if any "system" command...so the question; can someone help me code this in Perl please ... i have tried an now I am... (1 Reply)
Discussion started by: gvolpini
1 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

delete more than one line awk or perl

Hi all! How can a file be rid of three lines in sequence like the sample below: ... </s> <s> <w></w> </s> <s> ...to get: ... </s> <s> ... Note that the digits between square brackets may be more than one, comprising a comma, or a full-stop; and that the string between brackets... (1 Reply)
Discussion started by: mjomba
1 Replies

7. Shell Programming and Scripting

Help: how to convert perl script to awk in windows2003 server environment

For the following perl script, can anyone help to convert it to awk statement in windows2003 server environment ? Code: foreach $k (sort {$a <=> $b} keys %psnum) (1 Reply)
Discussion started by: tojzz
1 Replies

8. Shell Programming and Scripting

awk convert from line to column

i have an output like this : 012008 25760883 022008 12273095 032007 10103 032008 10115642 042007 20952798 but i would like to have it like this 012008,25760883 022008,12273095 032007,10103 032008,10115642 042007,20952798 (4 Replies)
Discussion started by: jarmouda
4 Replies

9. Shell Programming and Scripting

awk to perl convert

Dear Collegue Do anybody help me to convert this AWK script to Perl script { for (i = 2; i <= length ($0); i++) { x = substr($0, i , 1) if (c > 0) { b = b x if (x == "(") c++ ... (2 Replies)
Discussion started by: jaganadh
2 Replies

10. Shell Programming and Scripting

Delete the last line in a file using AWK or PERL

Is it possible to delete the last line in a file usin awk or perl? (4 Replies)
Discussion started by: suman_jakkula
4 Replies
Login or Register to Ask a Question