perl as gawk one liner

Is it possible to write the following gawk statement in a one liner perl? The system I am on does not have gawk.

gawk 'FNR==NR{a[$1]=$2;next}{print $0,a[$0]?a[$0]:"NA"}' file1 file2

Open in new window


File1:
host1 1.1.1.1
host2 1.1.1.2
host3 1.1.1.3
host4 1.1.1.4

File2:
host1
host3

Expected output:
host1 1.1.1.1
host3 1.1.1.3
LVL 21
MazdajaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
perl -alne '@ARGV?($a{$F[0]}=$_):print $a{$F[0]}||"$_ NA"' file1 file2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MazdajaiAuthor Commented:
Can you explain the use of || here?

 I tried to convert with a2p and is totally different.

#!/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

$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator

line: while (<>) {
    chomp;	# strip record separator
    @Fld = split(' ', $_, -1);
    if (($.-$FNRbase) == $.) {
	$a{$Fld[(1)-1]} = $Fld[(2)-1];
	next line;
    }
    print $_, $a{$_} ? $a{$_} : 'NA';
}
continue {
    $FNRbase = $. if eof;
}

Open in new window

0
ozoCommented:
I did change the behaviour slightly in making it a one liner, but it should only make a difference if you have a different number of files, or a different number of fields on a line.
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ozoCommented:
$a{$_} ? $a{$_} : 'NA'
might also be written as
 $a{$_} || 'NA'
but
$a{$_} ? $a{$_} : 'NA'
is a more literal translation of
a[$0]?a[$0]:"NA"
0
ozoCommented:
This should more faithfully reproduce the behaviour of the original awk command in the case where you have a different number of files or a different number of fields
perl -alne 'if(eof..0){print "$_ ",$a{$_}||"NA"}else{$a{$F[0]}=$F[1]}'
0
ozoCommented:
If you have a2p, you could create a literally equivalent one liner just by jamming everything into one command line, but I interpreted one liner to be a request to do it in a single perl statement.

the purpose of the
continue {
    $FNRbase = $. if eof;
}
was to allow perl to keep track of the difference between awk's NR and FNR
but the only thing the awk script used NR and FNR for was to distinguish between file1 and file2
http:#a39548377 used @ARGV for that purpose, since it was simpler, but that only gives an equivalent answer when you start with exactly two files.
0
MazdajaiAuthor Commented:
Yes I would like a single perl statement, but a2p seems to be over killing.

The first one is a clean, short form but I have a hard time interpreting it. The second one is longer but I found easier to understand.

perl -alne 'if(eof..0){print "$_ ",$a{$_}||"NA"}else{$a{$F[0]}=$F[1]}' 

Open in new window


Going to need some time to learn the shorter form, tyvm!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.