[Solved] Adding a column.


 
Thread Tools Search this Thread
Operating Systems HP-UX [Solved] Adding a column.
# 1  
Old 09-07-2012
Error [Solved] Adding a column.

Hi,

Please see the output below.

Code:
CPU TTY     PID USERNAME PRI NI   SIZE    RES STATE    TIME %WCPU  %CPU COMMAND
 0   ?       92 root     152 20   144K   128K run    667:31  1.85  1.85 fcachedaemon
22   ?    10957 patrol   154 30 68036K 35704K sleep  571:43  1.53  1.53 PatrolAgent
 0 pts/5  18237 bea      152 20  2500M  1822M run      3:49  1.20  1.20 java

I wish to get the sum of SIZE after trimming the 'K' letter at the end.
# 2  
Old 09-07-2012
See the input you have provided. It has size in MB and KB also...

So it's better to get size in bytes then add and convert to readable format....

Quote:
Originally Posted by mohtashims

Code:
CPU TTY     PID USERNAME PRI NI   SIZE    RES STATE    TIME %WCPU  %CPU COMMAND
 0   ?       92 root     152 20   144K   128K run    667:31  1.85  1.85 fcachedaemon
22   ?    10957 patrol   154 30 68036K 35704K sleep  571:43  1.53  1.53 PatrolAgent
 0 pts/5  18237 bea      152 20  2500M  1822M run      3:49  1.20  1.20 java

I wish to get the sum of SIZE after trimming the 'K' letter at the end.
# 3  
Old 09-07-2012
Error

Quote:
Originally Posted by pamu
See the input you have provided. It has size in MB and KB also...

So it's better to get size in bytes then add and convert to readable format....
Its an output of the top command. How can I get it in bytes format ?
# 4  
Old 09-07-2012
Assuming the SIZE (haven't used top command) shows values in K,M & G, this should work:
Code:
top|awk 'BEGIN {kmult=1024;mmult=kmult*1024;gmult=mmult*1024}
NR>1{
if($7~/K$/){totsiz+=($7+0)*kmult;next}
if($7~/M$/){totsiz+=($7+0)*mmult;next}
if($7~/G$/){totsiz+=($7+0)*gmult;next}
totsiz+=($7+0)}
END{print "Total Size : " totsiz/mmult " MB"}'

# 5  
Old 09-07-2012
Assuming you have size upto only MB's..

Output will be printed in KB's.Smilie

Code:
top |  awk '{ if ($7 ~ /K/) { {sum+=($7*1024)}}else{if ($7 ~ /M/){ {sum+=($7*1024*1024)}} else { sum+=$7}}}END { print (sum/1024) "K"}' file

just came to know the power awk...Smilie

Last edited by pamu; 09-07-2012 at 04:24 AM.. Reason: small change
# 6  
Old 09-07-2012
Question

Quote:
Originally Posted by elixir_sinari
Assuming the SIZE (haven't used top command) shows values in K,M & G, this should work:
Code:
top|awk 'BEGIN {kmult=1024;mmult=kmult*1024;gmult=mmult*1024}
NR>1{
if($7~/K$/){totsiz+=($7+0)*kmult;next}
if($7~/M$/){totsiz+=($7+0)*mmult;next}
if($7~/G$/){totsiz+=($7+0)*gmult;next}
totsiz+=($7+0)}
END{print "Total Size : " totsiz/mmult " MB"}'

I am getting the below error executing your command.

Code:
==> ./toptest.sh
awk: Input line
System: hh cannot be longer than 3,000 bytes.
                                              The source line number is 1.
                                                                          hht011f2:/home/users/bea>

---------- Post updated at 03:31 AM ---------- Previous update was at 03:28 AM ----------

Quote:
Originally Posted by pamu
Assuming you have size upto only MB's..

Output will be printed in KB's.Smilie

Code:
top |  awk '{ if ($7 ~ /K/) { {sum+=($7*1024)}}else{if ($7 ~ /M/){ {sum+=($7*1024*1024)}} else { sum+=$7}}}END { print (sum/1024) "K"}' file

just came to know the power awk...Smilie
I got this output

Code:
6.22216e+07K

Now how to I convert this in MBs and GBs ?
# 7  
Old 09-07-2012
Quote:
Originally Posted by mohtashims
Code:
6.22216e+07K

Now how to I convert this in MBs and GBs ?
Just replace the last part in the script mentioned in RED.
Code:
top |  awk '{ if ($7 ~ /K/) { {sum+=($7*1024)}}else{if ($7 ~ /M/){ {sum+=($7*1024*1024)}} else { sum+=$7}}}END { print (sum/1024) "K"}' file

#For MB's
{ print (sum/(1024*1024)) "MB"}

#For GB's
{ print (sum/(1024*1024*1024)) "GB"}

This User Gave Thanks to pamu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding values of a column based on another column

Hello, I have a data such as this: ENSGALG00000000189 329 G A 4 2 0 ENSGALG00000000189 518 T C 5 1 0 ENSGALG00000000189 1104 G A 5 1 0 ENSGALG00000000187 3687 G T 5 1 0 ENSGALG00000000187 4533 A T 4 2 0 ENSGALG00000000233 5811 T C 4 2 0 ENSGALG00000000233 5998 C A 5 1 0 I want to... (3 Replies)
Discussion started by: Homa
3 Replies

2. Linux

[SOLVED] Trouble adding Tun module

So I have Ubuntu running on a sheevaplug develop kit, but the damn kernel thing doesn't come with /dev/tun enabled. It was suggested that I look into using module assistant to compile & get this set up but I'm having issues. Linux ubuntu 2.6.39.4 #2 PREEMPT Fri Aug 5 19:05:11 MDT 2011 armv5tel... (4 Replies)
Discussion started by: dpreviti
4 Replies

3. Hardware

[solved] Sun Netra X1 - Adding a Second Hard Drive

As the title suggests, I'm trying to install a second drive (really want an OS mirror) on a Sun Netra X1. I've taken the spacer out, and had a go at with the drill-press so now I have a nice HDD tray. Have installed an IDE drive in the tray, plugged in the power and data cables that were... (0 Replies)
Discussion started by: Smiling Dragon
0 Replies

4. Shell Programming and Scripting

[Solved] Password query even after adding public key

Dears I am running a shell script to backup (transfer) files to a networked External HDD. Even though the public key has been added it still asks for the password before starting the transfer. Any suggestions? Thanks in advance. (8 Replies)
Discussion started by: BrownBob
8 Replies

5. Shell Programming and Scripting

[Solved] Sorting a column based on another column

hello, I have a file as follows: F0100010 A C F0100040 A G BTA-28763-no-rs 77.2692 F0100020 A G F0100030 A T BTA-29334-no-rs 11.4989 F0100030 A T F0100020 A G BTA-29515-no-rs 127.006 F0100040 A G F0100010 A C BTA-29644-no-rs 7.29827 F0100050 A... (9 Replies)
Discussion started by: Homa
9 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Deleting all rows where the first column equals the second column

Hi, I have a tab delimited text file where the first two columns equal numbers. I want to delete all rows where the value in the first column equals the second column. How do I go about doing that? Thanks! Input: 1 1 ABC DEF 2 2 IJK LMN 1 2 ZYX OPW Output: 1 2 ZYX OPW (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

[Solved] adding email option to KSH

Hi, I wanted to add a email option to this script. and was wondering if anyone could help me out. #!/bin/ksh echo "Finding hdisk" <DIR>/find-disk i=1 b=0 p=0 while ... (2 Replies)
Discussion started by: vpundit
2 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Adding time stamp to file name

I have the awk awk -F\* '$1=="ST",$1=="SE"{if($1=="ST"){close(f);f="sample" ++i} ; $1=$1; print>f}' OFS=\| <filename> How to add the time stamp to the file name mentioned as "sample" Please view this code tag video for how to use code tags when posting code and data. (6 Replies)
Discussion started by: atlantis_yy
6 Replies

9. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

10. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies
Login or Register to Ask a Question