CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Using perl script to replace sed command

 



sticko
Novice

Jun 26, 2014, 3:55 PM

Post #1 of 15 (3185 views)
Using perl script to replace sed command Can't Post

Beginner in perl scripts, I want to write a script to replace a sed command. I read perl has a sed like way that can do this. Looking for some help to get started.

This is the sed command I want to replicate:
sed -e 's/^/| /' -e 's/,/,| /g' -e 's/$/,|/' input.txt | column -ts,





So far I have this, but having trouble implementing the TODO:

#!/usr/bin/perl
use strict;
use warnings;

//TODO - replacement for sed command, pipe input.txt's output to input_temp.txt
open file1 "<input.txt";
while (<FH>)
{
// sed replacement
}



@ARGV = ('input_temp.txt', 'forecast.txt');

open SEL, '>', 'concatenate.txt' or die $!;
while (<>) {
print SEL;
}


Any help appreciated!


Laurent_R
Veteran / Moderator

Jun 26, 2014, 11:16 PM

Post #2 of 15 (3166 views)
Re: [sticko] Using perl script to replace sed command [In reply to] Can't Post

Take a look at the s2p (sed to Perl) utility.
: http://perldoc.perl.org/s2p.html


sticko
Novice

Jun 27, 2014, 10:06 AM

Post #3 of 15 (2788 views)
Re: [Laurent_R] Using perl script to replace sed command [In reply to] Can't Post

myfile:
sed -e 's/^/| /' -e 's/,/,| /g' -e 's/$/,|/' input.txt | column -ts,

>s2p -f myfile
s2p: myfile l.1: extra characters after command ('s/,/,| /g' -e 's/$/,|/' input.txt | column -ts,)


s2p doesn't seem to work.

I have tried in my script.pl:
#!/usr/bin/perl -w
system ("sed -e 's/^/| /' -e 's/,/,| /g' -e 's/$/,|/' input.txt | column -ts,");

> perl test.pl
sed: -e expression #3, char 2: unterminated `s' command

doesn't seem to work either. I think it's having trouble taking in multiple search/replace. Does perl handle linux's column command?


(This post was edited by sticko on Jun 27, 2014, 10:08 AM)


FishMonger
Veteran / Moderator

Jun 27, 2014, 10:27 AM

Post #4 of 15 (2772 views)
Re: [sticko] Using perl script to replace sed command [In reply to] Can't Post

Is this what you're looking for?


Code
perl -pe 's/^/| /; s/,/,| /g; s/$/,|/;' input.txt | column -ts


I'm not sure what the column command does so I can't say how to replicate its effect in perl.

However, the printf() or pack() functions can be used to output columnar data.


FishMonger
Veteran / Moderator

Jun 27, 2014, 10:31 AM

Post #5 of 15 (2768 views)
Re: [sticko] Using perl script to replace sed command [In reply to] Can't Post

Please provide a sample of your input data and the expected output.


sticko
Novice

Jun 27, 2014, 10:43 AM

Post #6 of 15 (2756 views)
Re: [FishMonger] Using perl script to replace sed command [In reply to] Can't Post

Hi FishMonger,

Basically I want to convert a CSV list into a nice table.

input.txt:

Code
student, breakfast, lunch, dinner, snack 
anna, YES, YES, YES, NO
chelsea, YES, NO, NO, NO
rachael, NO, NO, NO, YES


The linux 'column' command makes it in a nice table. When I use command line:
sed -e 's/^/| /' -e 's/,/,| /g' -e 's/$/,|/' input.txt | column -ts,

This is the generated output:

Code
| student                                 |  breakfast  |  lunch      |  dinner    |  snack      | 
| anna | YES | YES | YES | NO |
| chelsea | YES | NO | NO | NO |
| rachael | NO | NO | NO | YES |



(This post was edited by FishMonger on Jun 27, 2014, 11:11 AM)


FishMonger
Veteran / Moderator

Jun 27, 2014, 11:15 AM

Post #7 of 15 (2729 views)
Re: [sticko] Using perl script to replace sed command [In reply to] Can't Post


Code
D:\test>perl -laF",\s" -ne "printf(qq(| %-25s | %-10s | %-10s | %-10s | %-10s\n), @F)" input.txt 
| student | breakfast | lunch | dinner | snack
| anna | YES | YES | YES | NO
| chelsea | YES | NO | NO | NO
| rachael | NO | NO | NO | YES


Change the double quotes to single quotes if running on *nix system.


FishMonger
Veteran / Moderator

Jun 27, 2014, 11:21 AM

Post #8 of 15 (2725 views)
Re: [sticko] Using perl script to replace sed command [In reply to] Can't Post

Another option would be to use the Text:Table module.
http://search.cpan.org/~shlomif/Text-Table-1.130/lib/Text/Table.pm


sticko
Novice

Jun 27, 2014, 2:02 PM

Post #9 of 15 (2602 views)
Re: [FishMonger] Using perl script to replace sed command [In reply to] Can't Post

Thank you, that is exactly how I want the output to be.

Though one line command is convenient, it is hard to remember. I want to put this in a perl script file, so that I can just execute like:

> perl script.pl -f input.txt

How can I modify this into a script file for execution?


FishMonger
Veteran / Moderator

Jun 27, 2014, 3:36 PM

Post #10 of 15 (2494 views)
Re: [sticko] Using perl script to replace sed command [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Getopt::Long;

GetOptions("file|f=s" => \my $file) or die "Error in command line args\n";

$file or die "Usage: $0 -f <filename>\n $0 -file <filename>\n";
open my $fh, '<', $file or die "failed to open '$file' <$!>";

print '-' x 71, "\n";
while (<$fh>) {
chomp;
printf("| %-15s | %-10s | %-10s | %-10s | %-10s |\n", split /\s*,\s*/);
print '-' x 71, "\n" if $. == 1;
}
print '-' x 71, "\n";
close $fh;



Laurent_R
Veteran / Moderator

Jun 28, 2014, 3:36 AM

Post #11 of 15 (2020 views)
Re: [sticko] Using perl script to replace sed command [In reply to] Can't Post

Although providing the input and the desired output was much more efficient that asking for the translation into Perl of your command line, and you now have been provided satisfactory answers, I want to come back to this post or yours:


In Reply To
myfile:
sed -e 's/^/| /' -e 's/,/,| /g' -e 's/$/,|/' input.txt | column -ts,

>s2p -f myfile
s2p: myfile l.1: extra characters after command ('s/,/,| /g' -e 's/$/,|/' input.txt | column -ts,)


s2p doesn't seem to work.


the s2p utility does work, you just did not use it correctly. You just can't expect s2p to translate the "input.txt | column -ts," end of your command line, since this no longer sed code, nor the "sed -e" at beginning, because this is not yet sed code.

For example, trying this shell command:

Code
$ s2p -e 's/^/| /' -e 's/,/,| /g' -e 's/$/,|/'

would have given you a full (and pretty complicated) Perl script.

Having said that, the solution given by FishMonger after you presented the input and the desired output is much simpler and much better.


sticko
Novice

Jul 2, 2014, 10:58 AM

Post #12 of 15 (776 views)
Re: [FishMonger] Using perl script to replace sed command [In reply to] Can't Post

Thanks FishMonger, output is as intended. When I run with use warnings; it gives warning on this part of the code.


In Reply To

Code
 
printf("| %-15s | %-10s | %-10s | %-10s | %-10s |\n", split /\s*,\s*/);




Use of uninitialized value in printf at script.pl line 18, <$fh> line 2.
Use of uninitialized value in printf at script.pl line 18, <$fh> line 3.
...


Is it complaining about the split portion?

Thanks Laurant for showing how to use s2p!


FishMonger
Veteran / Moderator

Jul 2, 2014, 11:37 AM

Post #13 of 15 (773 views)
Re: [sticko] Using perl script to replace sed command [In reply to] Can't Post

Take a look at lines 2 and 3 of your input file. Do they hold a string in the format you're expecting i.e., a comma separated list of at least 5 fields?


(This post was edited by FishMonger on Jul 2, 2014, 11:38 AM)


Laurent_R
Veteran / Moderator

Jul 2, 2014, 11:56 AM

Post #14 of 15 (769 views)
Re: [FishMonger] Using perl script to replace sed command [In reply to] Can't Post

Possibly even simply two empty lines at the end of your file.


sticko
Novice

Jul 2, 2014, 12:48 PM

Post #15 of 15 (764 views)
Re: [Laurent_R] Using perl script to replace sed command [In reply to] Can't Post

Thanks for the quick replies! The SNACK column on lines 2,3 had <blank> data, so it didn't have 5 columns. Fixed, Thanks again!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives