![]() |
|
|
| Subject:
Perl One liners
Category: Computers > Programming Asked by: nemrod-ga List Price: $3.00 |
Posted:
16 Sep 2005 19:02 PDT
Expires: 16 Oct 2005 19:02 PDT Question ID: 568957 |
I have a list of integers in column in a text file. 2 34 787 0 5 3 0 123 4 22 ... I need to sum up all F[3] and print the total. How can you do that with a perl one liner? |
|
| There is no answer at this time. |
|
| Subject:
Re: Perl One liners
From: bozo99-ga on 17 Sep 2005 05:59 PDT |
Remebering that the columns are numbered from 0, here you go.
$ cat IN
2 34 787 0 5
3 0 123 4 22
$ perl -e 'while (<>) {@_=split(/\s+/); $sum+=$_[3]} print "$sum\n"' < IN
4 |
| Subject:
Re: Perl One liners
From: uncle_ziba-ga on 17 Sep 2005 07:05 PDT |
while bozo's solution works it can be more elegant. Why not use the
implicit loop and autosplit? like this:
perl -alne '$sum+=$F[3]; END {print $sum}' IN |
| Subject:
Re: Perl One liners
From: nemrod-ga on 21 Sep 2005 09:52 PDT |
Excellen solutions, thank you very much. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |