Perl script to replace awk

L

laura.hradowy

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
 
B

Bob Walton

(e-mail address removed) wrote:

....
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.
....


Check out:

perldoc a2p
 
T

Tad McClellan

I am just having a difficult time trying to take awk and convert to perl.


Run it through the awk-to-perl translator (a2p) that comes with perl itself.

man a2p
 
G

Geoffroy Braem

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
Hi Laura,

You could try this


#!/usr/bin/perl
print "Enter BLD:\n";
chomp($input1 = <STDIN>);
print "Enter room:\n";
chomp($input2 = <STDIN>);
open(TMP, "yourdatafile") or die $!;
while(<TMP>){
chomp;
@L = split /,/, $_;
print join(",", $L[0],$input1,$input2,@L[1..$#L]), "\n";
}






--

(e-mail address removed)
Logidox Computing
CM (Clearcase) & DataWarehouse(Oracle) consulting, Unix programming
(Java, Perl, Korn Shell).
in Belgium, England, Luxembourg and more.
www.logidox-computing.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top