Re: perl to run awk and sed
by stevieb (Canon) on Nov 30, 2015 at 17:32 UTC
|
perl can do all of this internally.
Why don't you show us a few lines of your input you're sending to these commands, your expected output after they're processed, and we'll see if we can avoid you shelling out?
Here's an attempt of what I think you want though:
use warnings;
use strict;
my @lines = (
# where '\t' is a real tab
"one\ttwo\tthree",
"four\tfive\tsix",
);
for my $line (@lines){
my $str = (split /\t/, $line)[0];
$str =~ s/.$//;
print "$str\n";
}
__END__
on
fou
| [reply] [d/l] |
|
|
I want to run the awk and sed on a file, which has the below
myserver.it.com. XXXX XXX XXX XXXXXXXXXXX
So I want to get the server names from a file and pipe this via awk and sed into another file
The awk is to get the first line (server name) and the sed is to get rid of the last. (full stop).
| [reply] |
|
|
use warnings;
use strict;
my @lines = (
"myserver.it.ca. XXXX XXX XXX XXXXXXXXXXX",
"myserver.it.org. XXXX XXX XXX XXXXXXXXXXX",
"myserver.it.com. XXXX XXX XXX XXXXXXXXXXX",
);
for my $line (@lines){
my $server = (split /\s+/, $line)[0];
$server =~ s/\.$//;
print "$server\n";
}
__END__
myserver.it.ca
myserver.it.org
myserver.it.com
| [reply] [d/l] |
|
|
|
|
|
|
|
|
| [reply] |
|
|
I d rather use perl where I can
++. I would also advise you to do it in pure Perl, which can do anything that awk and sed can do, and much more, and in most cases more efficiently and with much richer functionalities.
| [reply] |
|
|
Re: perl to run awk and sed
by runrig (Abbot) on Nov 30, 2015 at 21:08 UTC
|
If you want to convert awk or sed to perl, a2p and s2p can do that for you. You can use the results as is, or just get an idea of how to do something in perl. E.g. for your awk, a2p gives: $, = ' '; # set output field separator
$\ = "\n"; # set output record separator
$FS = "\t";
;
while (<>) {
chomp; # strip record separator
($Fld1) = split($FS, $_, -1);
print $Fld1;
}
s2p outputs more cruft, but the important bit is just 's/.$//', which can be inserted into the above script. | [reply] [d/l] |
|
|
| [reply] [d/l] |
Re: perl to run awk and sed
by toolic (Bishop) on Nov 30, 2015 at 17:31 UTC
|
One way might be something like this:
my $cmd = q(awk 'BEGIN {FS="\t"}; {print $1}' | sed 's/.$//');
system $cmd;
Single quotes prevent variable interpolation: see perlop | [reply] [d/l] |
|
|
| [reply] |
Re: perl to run awk and sed
by deelinux (Novice) on Nov 30, 2015 at 17:31 UTC
|
I've managed to answer it just after posting..., by adding a forward slash's before the variables the command use, so perl would not try and treat them as its variables..I think..., but it works, that said if any can point me to what perl guys would normally use they would use in place of awk/sed for me to do some reaserch on, that would be great
awk 'BEGIN {FS="\t"}; {print \$1}'| sed 's/.\$//'
| [reply] |
|
|
$ perl -wE 'q{myserver.it.com. XXXX XXX XXX XXXXXXXXXXX} =~ /^(.*?)\.\
+s/; say $1'
myserver.it.com
"... for me to do some reaserch"
Given the simplicity of that regex, further research may be unnecessary;
however, on the basis of "Im still new to perl and programming" (from your OP),
you might want to start with the very basics in "perlintro: Regular expressions".
At the end of that section, you'll find links to documentation with more detailed information.
| [reply] [d/l] [select] |