Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Editing just one column in a file

by ZWcarp (Beadle)
on Nov 28, 2011 at 21:51 UTC ( [id://940477]=perlquestion: print w/replies, xml ) Need Help??

ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:

Say I have a file with 30 columns and I want to add 1 to the number in column 15, but otherwise print the line as is. Is there a better way to do this then something like

perl -lane' print "$F[0]\t$F[2]\t$F[3]....\t" .$F[15]+1 . "\t$F[16] .....\t$F[30]"' file.txt

The dots representing $Fx\t ( or everything in-between) Any ideas? the shorter the better. Also i was thinking maybe a tricky substation might work but this could get difficult with more complex operations. Still.... open to any suggestions Thanks for your time.

Replies are listed 'Best First'.
Re: Editing just one column in a file
by BrowserUk (Patriarch) on Nov 28, 2011 at 21:54 UTC

    perl -lane '++$F[15]; print join "\t", @F' file

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Ahhh ok brilliant. One more question.. why does this only print one column instead of the specified array range?I'm sure I'm doing something stupid but I can't find a good website or text with detailed stuff on specific command line perl

      perl -lane '$,="\t";print $F[0..6]'  file.txt Thanks so much!

      UPDATE Sorry, I got it, you just need to change the $ to a @ in front of F. Apologies

      hi, i am confused about this, need your help.
      echo "Just another perl monkers " | perl -lane 'print $F[0..3]'
      Output:
      Just
      why it treat $F[0..3] as $F[0] but not $F[1]or $F[2]or $F[3].

        Because the Perl syntax for an array slice is @a[ ... ].

        If you enable warnings, you'll see that by using $a[1..3], the 1..3 is treated as a flip-flop operator comparing against $., and under most circumstances will produce false, which in the numeric context gets taken as 0:

        c:\test>perl -wE"@a = 0..10; say $a[1..3]; say @a[1..3]" Use of uninitialized value in range (or flip) at -e line 1. Argument "" isn't numeric in array element at -e line 1. 0 123

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://940477]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-16 19:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found