Operating System - HP-UX
1827703 Members
3001 Online
109967 Solutions
New Discussion

Re: Perl script to replace awk

 
SOLVED
Go to solution
Ratzie
Super Advisor

Perl script to replace awk

I and switching over to perl and need a starting point and need to write a script that will do the following...
I just need a kick, and should be on my way.

I have a file that is separated by commas, if I print the file out I would get this...
awk -F, '{print "204"$1","$2","$3}' FILE

The output is
204xxxx001,00 0 05 21,TELN NOT
204xxxx002,00 0 01 30,TELN NOT
204xxxx008,00 0 04 15,TELN NOT
204xxxx013,00 0 02 30,CUST HAS
204xxxx015,00 0 10 22,CUST HAS

I also need to insert data into this line...
204xxxx001,EAST_BLD,ROOM2,00 0 05 21
204xxxx002,EAST_BLD,ROOM2,00 0 01 30

I am just having a difficult time trying to take awk and convert to perl.
I know I can write in bash but I would like to try in perl.

How do I prompt user for input that will insert into the $2, as well as $3.

Basically I want,
Enter BLD:
User enter is EAST_BLD

Enter room:
User enters in ROOM2

Then the perl script takes that info and inserts these fields into every line, as well as adding 204 at the begining and dumping the last field.

And here is a trickier question...
How do I do a grep on CUST and then place those lines in a file
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: Perl script to replace awk

If you have existing awk scripts then your best bet is to run them through the perl utility a2p. a2p < myfile.awk > myfile.pl.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Perl script to replace awk

If you have existing awk scripts then your best bet is to run them through the perl utility a2p. a2p < myfile.awk > myfile.pl.

Man a2p for details.
If it ain't broke, I can fix that.
Ratzie
Super Advisor

Re: Perl script to replace awk

That would be great but this is where I get confused and run into a snag...

I can awk -F, '{print "204"$1","$2","$3}' FILE

But I can not...
echo "Enter BLD: "
read $BLD

Then insert it the awk statement...
awk -F, '{print "204"$BLD","$2","$3}' FILE

awk does not like it. Or am I doing something wrong with that.

Rodney Hills
Honored Contributor
Solution

Re: Perl script to replace awk

How about this-

#!/usr/bin/perl
print "Enter BLD:"; $bld= ; chomp $bld;
print "Enter ROOM:"; $room= ; chomp $room;
while() {
chomp;
@a=split(",",$_);
print join(",",$a[0],$bld,$room,$a[1],"\n" unless $a[2]=~/^CUST/;
}

HTH

-- Rod Hills
There be dragons...
Ratzie
Super Advisor

Re: Perl script to replace awk

Thank you works like a charm, got one more question for you how would I add 204 to the beginning of [0]


I dont know if I can explain this correctly by here goes.
I am trying to write a perl program that takes a file let say it looks
like this...
xxxx001,00 0 05 21,TELN NOT
xxxx002,00 0 01 30,TELN NOT
xxxx008,00 0 04 15,TELN NOT
xxxx013,00 0 02 30,CUST HAS
xxxx015,00 0 10 22,CUST HAS

I want to also insert a few things to this file...
I want to prompt user to enter in
print "Enter BLD:"; $bld= ; chomp $bld;
print "Enter ROOM:"; $ROOM= ; chomp $ROOM;

Also I want a areacode fixed to the first column so when all is said
and done, I have part of the script but can not add the areacode to
it???

#!/opt/perl/bin/perl
$acode="204";
print "Enter BLD:"; $bld= ; chomp $bld;
print "Enter ROOM:"; $room= ; chomp $room;
while() {
chomp;
@a=split(",",$_);
print join(",",$a[0],$bld,$room,$a[1],$a[2],$a[3],"\n") ;

How do I add 204 at the beginning of the [1]?

204xxxx001,EAST_BLD,ROOM1,00 0 05 21,TELN NOT
Ermin Borovac
Honored Contributor

Re: Perl script to replace awk

One way to do it is with the perl dot operator.

print join(",",$acode.$a[0],$bld,$room,$a[1],$a[2],$a[3],"\n");
Hein van den Heuvel
Honored Contributor

Re: Perl script to replace awk



Just to give you some other thinking directions:

# cat x.pl
$bld = shift (@ARGV);
$room = shift (@ARGV);
die "Usage: $0 []" unless $room;
$area = shift (@ARGV);
$area = "204" unless $area;
while (<>) {
($part1,$part2) = split (/,/);
print "$area$part1,$bld,$room,$part2\n";
}


# perl x.pl BLD ROOOOOM < x.dat
# perl x.pl BLD ROOOOOM 123 x.dat


fwiw,
Hein.