Urgent Awk manipulation query help


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Urgent Awk manipulation query help
# 1  
Old 08-14-2006
CPU & Memory Urgent Awk manipulation query help

Hi UNIX gurus Smilie ,
I have the following requirement and not being able to find a solution Smilie .

i have got a file demo.lst

movies 10 12 25
songs 11 8 15
DVD 15 12 10
Total 58 60 75


NOTE:-Total value not the sum of the columns but some predefined value.

My requirement is to create another row as follows and spool it in a file.

Final_Total x y z
where x value will be 58-(15+11+10)
where y value will be 60-(12+8+12)
where z value will be 75-(10+15+25)


thanks in advance,
Regards,
Rahul26
Smilie
# 2  
Old 08-14-2006
Are you sure this is not homework?

Maybe it can be done very easily with awk, but I'm not a power user of awk, you can give this a try:
Code:
#! /bin/ksh
nol=`wc -l demo.lst | awk '{print $1}'`
count=1
while read a x y z
do
        if [[ $count -lt $nol ]]; then
                tot_x="$tot_x+$x"
                tot_y="$tot_y+$y"
                tot_z="$tot_z+$z"
                let count=$count+1
        else
                break
        fi
done < demo.lst
tot_x=`echo $tot_x | sed 's/^+//'`
tot_y=`echo $tot_y | sed 's/^+//'`
tot_z=`echo $tot_z | sed 's/^+//'`
line=`tail -1 demo.lst`
ftot_x=`echo $line | awk '{print $2}'`
ftot_y=`echo $line | awk '{print $3}'`
ftot_z=`echo $line | awk '{print $4}'`
echo "Final_Total $ftot_x-($tot_x) $ftot_y-($tot_y) $ftot_z-($tot_z)" >> spoolfile

Regards,
Tayyab
# 3  
Old 08-14-2006
Hammer & Screwdriver

I think this can be helpful.........

cat file | awk '{
sum1=sum1+$2;
sum2=sum2+$3;
sum3=sum3+$4;
}
END { print "total: " sum1, sum2, sum3}' >> resultfile

regards
Apoorva kumar
# 4  
Old 08-14-2006
Quote:
Originally Posted by apoorvasharma80
I think this can be helpful.........

cat file | awk '{
sum1=sum1+$2;
sum2=sum2+$3;
sum3=sum3+$4;
}
END { print "total: " sum1, sum2, sum3}' >> resultfile

regards
Apoorva kumar
Please have a look at original post, OP has asked for something else.

Regards,
Tayyab
# 5  
Old 08-14-2006
Quote:
Originally Posted by shereenmotor
Are you sure this is not homework?

Maybe it can be done very easily with awk, but I'm not a power user of awk, you can give this a try:
Code:
#! /bin/ksh
nol=`wc -l demo.lst | awk '{print $1}'`
count=1
while read a x y z
do
        if [[ $count -lt $nol ]]; then
                tot_x="$tot_x+$x"
                tot_y="$tot_y+$y"
                tot_z="$tot_z+$z"
                let count=$count+1
        else
                break
        fi
done < demo.lst
tot_x=`echo $tot_x | sed 's/^+//'`
tot_y=`echo $tot_y | sed 's/^+//'`
tot_z=`echo $tot_z | sed 's/^+//'`
line=`tail -1 demo.lst`
ftot_x=`echo $line | awk '{print $2}'`
ftot_y=`echo $line | awk '{print $3}'`
ftot_z=`echo $line | awk '{print $4}'`
echo "Final_Total $ftot_x-($tot_x) $ftot_y-($tot_y) $ftot_z-($tot_z)" >> spoolfile

Regards,
Tayyab
Hi ,
Brialliant.........actually i was not knowing the use of READ and was looking for something similar.
newaz Thanks A Lot....It solved my purpose....

And its not homework stuff..........a real life requirement.


Regards,
Rahul26
# 6  
Old 08-17-2006
Something like this, maybe?
Filename:blk.awk
Code:
BEGIN {
        RS=""
}
{
  for(i=1; i <= NF; i++)
        {
        array[i]=$i
        }

        sum[0]=array[14]-(array[10]+array[6]+array[2])
        sum[1]=array[15]-(array[11]+array[7]+array[3])
        sum[2]=array[16]-(array[12]+array[8]+array[4])
        print "Sum: " sum[0],sum[1],sum[2]
}
END {
        printf ("\nDone :-) ")
}

Code:
awk -f blk.awk demo.lst

hth
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk manipulation

Hello all, Can someone help me with write part of code in awk to merge 2 files? Go through file1 check if number from column 3 exist in file2(column 2) if yes take value from column 1 and add to column 4 in file1. If value in column 4 exist in file1 skip it. file1... (2 Replies)
Discussion started by: vikus
2 Replies

2. Shell Programming and Scripting

awk manipulation

hello I have example file AA 11 BB 22 CC 33 And what I expect to have -a AA=11 -a BB=22 -a CC=33 can anyone help how I have this using awk? (1 Reply)
Discussion started by: vikus
1 Replies

3. Shell Programming and Scripting

awk manipulation

Hallo Family, I have csv file which has over a million records in it. All i want to do is to change field 2 to have the same value as field 10. sample file:Now 0860093239,Anonymous,unconditional,+27381230283,Anonymous,unconditional,y,public,,2965511477:0A Desired output: ... (2 Replies)
Discussion started by: kekanap
2 Replies

4. Shell Programming and Scripting

Very Urgent Query... PLease help me

Dear All, I need an output file in a way as described below the above data should be static i.e. will remain the same and the second feild will print a value comming from another script as per time (please note script will be scheduled for 30 min interval in crontab, so it will run at... (1 Reply)
Discussion started by: jojo123
1 Replies

5. Programming

urgent help with file manipulation program

Hey, i am trying to write a program that takes multiple files as command line arguments, then outputs them to a single file. i also need the option to read them back out again. essentially what i am trying to create is an archiver, however, a very simple one. The program i have accomplished so far... (1 Reply)
Discussion started by: wezzyb
1 Replies

6. Shell Programming and Scripting

$0 manipulation in awk

OK, so if $0 represent the entire record... can I change $2 and will that be reflected back in $0? I think the following answers that YES, it does work. But is there anything I should be thinking about prior to doing this? What I am actually doing is part of 5 pages of scripting and awk... (1 Reply)
Discussion started by: joeyg
1 Replies

7. Shell Programming and Scripting

Query regarding string Manipulation

Dear Experts, I have to write a shell script in which i get one string of numbers (e.g 1234567890) from user & i have to chage it to (2143658709). Please suggest how can i achieve the same. Rule is: In case the number of digits is even then translation should be: 123456 -> 214365 ... (3 Replies)
Discussion started by: dhiraj4mann
3 Replies

8. Shell Programming and Scripting

how to convert the result of the select query to comma seperated data - urgent pls

how to convert the result of the select query to comma seperated data and put in a .csv file using korn shell. Pls help me as its very urgent. Thanks, Hema. (1 Reply)
Discussion started by: Hemamalini
1 Replies

9. Shell Programming and Scripting

File manipulation with awk

Could you please help me to achieve the below: In a file I need to convert the multiple lines whose filed 1 and field 5 values are same into a single line but with the field 4 values comma separed as mentioned below. Fileds after 5 shall be discarded. Also here by default all other remaining... (6 Replies)
Discussion started by: dhams
6 Replies

10. Shell Programming and Scripting

awk manipulation

Hi , what a wonderful command but so hard to maintain ! i have a file like that : 03/07/2006 05:58:45 03/07/2006 06:58:45 03/07/2006 07:58:50 03/07/2006 08:58:50 and i want to read it and keep only the lines with 3rd field less than 07:00:00 writing it in a second file ! ... (2 Replies)
Discussion started by: Nicol
2 Replies
Login or Register to Ask a Question