tags:

views:

85

answers:

4

I write the following script

  #!/usr/bin/perl

  system("  awk -F"=" '{s[$1]++}{print $1s[$1],$2}' OFS="=" /var/tmp/file " );

when I run the script I get:

      Can't modify string in scalar assignment at ./stam2 line 5,             near "" /var/tmp/file " )"
       Execution of ./stam2 aborted due to compilation errors.

what the problem here?

lidia

• the target of the script is to manipulate and add numbers after each parameter in line

+1  A: 

You're executing a statement that has the equals sign in it, as part of a system() call, which you can't do.

I suspect this might be what you want:

 #!/usr/bin/perl

  system( qq{  awk -F"=" '{s[$1]++}{print $1s[$1],$2}' OFS="=" /var/tmp/file } );
godswearhats
I think you mean "the statement has quotes in it" , not "equals"
Kent Fredric
I get the errors? : awk: cmd. line:1: {s[]++}{print s[],}awk: cmd. line:1: ^ parse errorawk: cmd. line:1: fatal: invalid subscript expression
lidia
There's definitely confusion here. What is it you're trying to do exactly, and I'll give you a perl recipe for it? :-)
godswearhats
only I ask is why I can to run the perl with awk ?why the errorsplease help
lidia
@godswearhats If you want to give the OP an answer, check the other thread. I'm really hoping this one gets closed soonish...http://stackoverflow.com/questions/3290398/perl-numeration-word-or-parameter-in-file
Telemachus
A: 

From what I understand about the system command is that it takes a list of strings where each string is a single command you are running. Already you have issues because you have operators not surrounded by the quotes. I would try this:

system("awk -F\"=\" '{s[$1]++}{print $1s[$1],$2}' OFS=\"=\" /var/tmp/file");

However it seems what you really want to do can easily be done with perl's split and joins. Something along the lines of:

open my $fh, '<', "/var/tmp/file" or die "could not open for read.";
open my $output, '>', "/var/tmp/outfile" or die "could not open for write.";
while(<$fh>){
    my $line = $_;
    my @fields = split(/\=/, $line);
    for my $field (@fields){
        $field++;
    }
    print $output join(";", @fields);
}

Of course there may be a more elegant way do this such as with inline editing but this is to give you an idea. Updated for three argument open

stocherilac
I have probelm the /var/tmp/outfile not created why?
lidia
Bad @stocherliac, use 3 arg open, PLEASE.`open my $fh, '<', "/var/tmp/file" or die $@;` `open my $fh, '>', "/var/tmp/outfile" or die $@;`
Kent Fredric
@Kent you're right, that was very sloppy of me. I was rushing out the door to lunch, will edit.
stocherilac
@lidia this should be solved with the three argument open. You might also consider '>>' for you're second argument in the open for the output file. See http://perldoc.perl.org/functions/open.html for more info.
stocherilac
A: 

You're not escaping or quoting your quotes.

The literal string " awk -F" begins with a double quote and last only until it sees another un-escaped double quote. So it's done at F". If you put a = after that then it's entirely a different token. You are assigning.

The reason that the error message says what it says is that assignment goes from right to left. Look further down your line and you'll see

"...OFS="=" /var/tmp/file "

That's the assignment it starts with. Then it looks leftward and sees that you are assigning it to an assignment. And gives you the error you're getting.

Perl will let you put double quotes in a interpolated string (one that allows the "stringification" of variables) which normally are delineated by double quotes. But you need to use qq:

Look closely at the coloring between the two lines:

system("  awk -F" = " '{s[$1]++}{print $1s[$1],$2}' OFS=" = " /var/tmp/file " );
system( qq(awk -F"=" '{s[$1]++}{print $1s[$1],$2}' OFS="=" /var/tmp/file));

Look very closely at the first line, notice that the rest of the string is reddish while the equals are black. It's jumped out of the string and you're dealing with = as a new token.

Now, SO highlighting doesn't deal too well with all of Perl's ability, so the qq( operator isn't highlighted correctly. But if you ignore that, you'll see that all the stuff in quotes shows up as in quotes. And if you trust that qq can do the job of passing it all as a string, then you'll trust that it's all the SAME grammatical unit.

Axeman
+1  A: 

Perl comes with a program called a2p, that translates awk to Perl. You may find that to be a better solution than invoking awk from Perl.

ninjalj