1

foo.txt:

Lillypaul_sg
204803
204803
204803
Ammy_sg
3
ramaswamy_sg
3
33
tommy_sg
3
3
137374
100

Desired Out.txt :

Lillypaul_sg
614409
Ammy_sg
3
ramaswamy_sg
36
tommy_sg
137480

The output is a sum of values in the input file. Headers may contain alpha numeric values but always ends with "_sg".

  • How can we know what is a header and what a value? Will all headers be only letters and _ and all values be numerical? Will all headers contain a _? Can we use that to identify them? Also, please show us i) what you've done so far so we don't give you solutions you've already tried and ii) your actual desired output, including the leading 0s you mention so we know exactly what you need. – terdon Jun 11 '15 at 22:28
  • Thanks for the edit. Do you no longer want the numbers to be 0-padded? – terdon Jun 11 '15 at 22:33
  • I changed the question description. I just need the addition of values between each header. No need to append with leading 0's. – ayrton_senna Jun 11 '15 at 22:33
3
awk '$0 == $0+0{
                summ += $0
                next}
               {
                if(summ)
                    format="%06d\n%s\n"
                else
                    format="%s%s\n"
                printf format, summ, $0
                summ=""}
     END       {
                if(summ)
                    printf "%06d\n", summ}' foo.txt
  • Cant i do this in a One line command with out using a script? i prefer to use multiple awks with pipe '|'. Can you do it like that? – ayrton_senna Jun 11 '15 at 22:37
  • @ayrton_senna This is a one liner. Just paste it directly into your terminal. Costas has just expanded it for readability but it's the same as awk '$0==$0+0{summ += $0;next}{if(summ){format="%06d\n%s\n"}else{format="%s%s\n"} printf format, summ, $0; summ=""} END{if(summ) print summ}' file – terdon Jun 11 '15 at 22:41
  • @ayrton_senna It is online command. The newline is the same as ;. Or you can store all between ' as separate file and call awk -f separate.file foo.txt – Costas Jun 11 '15 at 22:42
  • @terdon Perfect!!. Thank you. Whenever i see an expanded version of one liner commands, i try to paste them in note pad and adjust it as one line. But due to my lack of knowledge on syntax. i always makes mistakes in rearranging them into 1 line. Your comment helped me. I will practise more. – ayrton_senna Jun 11 '15 at 22:46
  • @ayrton_senna You can put expanded command directly into shell – Costas Jun 12 '15 at 12:21
1

In Perl:

$ perl -lne 'if(/_sg/){print "$n\n$s" if defined($n); $n=$_; $s=0;}
             else{$s+=$_}END{print "$n\n$s"}' file 
Lillypaul_sg
614409
Ammy_sg
3
ramaswamy_sg
36
tommy_sg
137480

If you want the numbers to be 0-padded if they're less than 6 digits (as in your original question):

$ perl -lne 'if(/_sg/){printf "%s\n%0.6d\n",$n,$s if defined($n); $n=$_; $s=0;}
             else{$s+=$_}END{printf "%s\n%0.6d\n",$n,$s}' file 
  • i can not use perl – ayrton_senna Jun 11 '15 at 22:39
  • @terdon - the q is tagged solaris - probably no perl. – mikeserv Jun 11 '15 at 22:42
  • 1
    @mikeserv yeah, saw that after posting. Costas already gave a fine awk solution so I'll just leave this here in case the next person to stumble across it has a proper OS ;) – terdon Jun 11 '15 at 23:01
  • @terdon - tell me about it. considering how much it costs, you'd figure the Sun folks would provide code which has seen the light of a text editor since the 80s. And that is a good answer - glad to see a post from him, too. – mikeserv Jun 11 '15 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.