Need comand or script for append text after searching for the desired string

Hi all,
i have a generated report in unix in the following command like

  input.txt
  47.85,10
  0124,42.35,8
  0125,3.5,2

the input file format is fixed
I need the my output file with append text as below

  output.txt
  0124 amount:42.35
  0125 amount:3.5
  0124 count : 8
  0125 Count : 2
  total amount :47.85
  total count : 10

regards,
hemanth saikumar

What have you tried so far? Why don't you use code tags as required by forum rules? You're with us for more than a year and a half!

i have joined again in this forum one day back ..as iam in different stream iam unable to follow this forum ....so... if u know please guide me...

regards,
vasa saikumar.

OK, understood. But still: what did you try, and why no code tags?

iam generating the input file through a tool and storing it in the unix server now i have to append the text as i mentioned below
output.txt

  0124 amount:42.35
  0125 amount:3.5
  0124 count : 8
  0125 Count : 2
  total amount :47.85
  total count : 10

Please use code tags: highlight the contents of output.txt and click the editor's code button. This is mentioned in clear text just above the editor window.

For your problem (which you do not seem to have tackled yourself yet), try

awk -F, 'NF<3   {next}
                {ITM[NR]=$1
                 AMT[NR]=$2
                 CNT[NR]=$3
                 TOTA+=$2
                 TOTC+=$3}
         END    {for (i=2;i<=NR;i++) print ITM " amount:" AMT
                 for (i=2;i<=NR;i++) print ITM " count:" CNT
                 print "total amount:" TOTA 
                 print "total count:" TOTC} 
        ' file
0124 amount:42.35
0125 amount:3.5
0124 count:8
0125 count:2
total amount:45.85
total count:10

I actually don't understand how your total amount of 47.85 is calculated...

1 Like

hi thanks for the code i am using my tool to calculate the total...

had one doubt i have to do null check for the same
like if i had

  0125 amount:0.00
  0125 count:0

then those particular things shouldnot be get printed
like my output should ne

 
output.txt
  0124 amount:42.35
    0124 count : 8
  total amount :47.85
  total count : 10

That totals can't possibly be correct! Don't you mistrust your "tool" with results like that?

Neglect item if either or both amount and count are 0?

ya ,
and my output should be (after amount text 0124 should get appended):mad::mad:
output.txt

 amount 0124:42.35
 count  0124 : 8

try this code

LANG=C  awk 'NR==1{split($0,a,",")}NR==2{split($0,b,",")}NR==3{split($0,c,",")}END{print b[1],"amount:",b[2];if (c[2]!=0){print c[1],"amount:",c[2]};print b[1],"count:",b[3];if(c[3]!=0){print c[1],"count:",c[3]};print "total amount:",b[2]+c[2],ORS,"total count:",a[2]}' file

hi thanx for the reply ,

i need the below format (after amount "0124" should get appended ).. :mad::mad:

amount 0124:42.35
amount 0125:3.5
count 0124: 8
Count 0125 : 2
total amount :47.85
total count : 10 
  

What about putting in some efforts yourself?

Try

awk -F, 'NF<3   {next}
         $2+$3  {ITM[++c]=$1
                 AMT[c]=$2
                 CNT[c]=$3
                 TOTA+=$2
                 TOTC+=$3}
         END    {for (i=1;i<=c;i++) print "amount " ITM ":" AMT
                 for (i=1;i<=c;i++) print " count " ITM ":" CNT
                 print "total amount:" TOTA
                 print "total count:" TOTC}
        ' file
amount 0124:42.35
 count 0124:8
total amount:42.35
total count:8

hi Rudic ,
i had a requirement like if amount 0124 is null then i have to append 0.00 similarly if i had count as zero then count 0124 as 0

0124 amount:0.00
0125 amount:3.5
0124 count:8
0125 count:0
total amount:3.5
total count:8

Please specify the conditions (see post#8).

hi Rudic,
please find the below input file

   47.85,10
   0124,42.35,8
   0125
  

My output should be

amount 0124:42.35
amount 0125: 0.00
count 0124 : 8
count 0125 : 0

What about your specification in post#7? And where are the totals?

sorry ... rest of all the things are same....

 amount  0124:42.35
 amount  0125:0.0
   count  0124 : 8
   Count  0125: 0
  total amount :42.35
  total count : 8

Try this:

awk -F, 'NF==2  {next}
                {ITM[++c]=$1
                 AMT[c]=$2+0
                 CNT[c]=$3+0
                 TOTA+=$2
                 TOTC+=$3}
         END    {for (i=1;i<=c;i++) print "amount " ITM ":" AMT
                 for (i=1;i<=c;i++) print " count " ITM ":" CNT
                 print "total amount:" TOTA
                 print "total count:" TOTC}
        ' file
amount    0124:42.35
amount    0125:0
 count    0124:8
 count    0125:0
total amount:42.35
total count:8
1 Like

u Rocked man.... thanks for helping me from the starting ... due to you iam getting kinda knowledge in unix....

Rudic... one more help do u have any idea on SFTP from windows to unix...?

I've seen your other thread - please give way more details and provide some attempts that you have done.