Conversion if 1st column is match (awk '{s+=$1} END


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conversion if 1st column is match (awk '{s+=$1} END
# 1  
Old 03-13-2014
Conversion if 1st column is match (awk '{s+=$1} END

Hi im trying to add numbers, got no problem with it im using
Code:
 awk '{s+=$1} END {print s, "MB"}',

but what if the numbers are like mention below. who will i add them

Code:
2000 KB
1 MB
Answer: 2001

Desired:
2000 KB
1 MB

Answer: 3000


Last edited by vbe; 03-13-2014 at 12:10 PM.. Reason: extra code tags...
# 2  
Old 03-13-2014
Check $2 value and perform required arithmetic:
Code:
awk '{s+=($2=="MB"?$1*1024:$1)}END{print s " MB"}' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-13-2014
Well so far and your subject are clear on what you did: Considering only column 1...
So your "what if"... would be to look also at column 2, and depending what you find, add a conversion to the value in column 1...
This User Gave Thanks to vbe For This Post:
# 4  
Old 03-13-2014
@VBE, yeah my bad,, was creating a script that will add my total diskpace.ttal memory,nuber of cpu, Flavor of OS, cpu model, server serial... this is my lack, the total disk space,

@yoda
thanks, big help for my basic script but will reduce a lot of time and effort
# 5  
Old 03-13-2014
Code:
awk '                                                  
$2~/^[Kk]/ {s+=$1/1024}
$2~/^[Mm]/ {s+=$1}
$2~/^[Gg]/ {s+=$1*1024}
END {print s,"MB"}
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

3. Shell Programming and Scripting

Search from 1st match and end 2nd match

I've been looking through the forums for awhile now and looking at the man page for grep and egrep and not seeming to find this scenario so it might not be possible but figured I'd throw it out to get some ideas. I'm looking for a way to search a file for 1st match (example below net self) and... (3 Replies)
Discussion started by: djzah
3 Replies

4. Shell Programming and Scripting

Add column to end of each line with ´awk´

Hi, I have data with approximately 300 columns. I want to add a column to the end of each column with the value "1". Is there a way that I can do this is ´awk´ without having to specify each individual column. For instance, my data looks like: pvb 1 2 3 4 5 ....... 300 fdh 3 4 5 2 4 ......... (4 Replies)
Discussion started by: owwow14
4 Replies

5. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

6. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

Remove lines that match string at end of column

I have this: 301205 0000030000041.49000000.00 2011111815505 908 301205 0000020000029.10000000.00 2011111815505 962 301205 0000010000027.56000000.00 2011111815505 3083 312291 ... (2 Replies)
Discussion started by: herot
2 Replies

8. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

9. Shell Programming and Scripting

awk match second column

Hi I have file which looking like: fox_spectrum fox_spectrum\ fox_spectrum (fox_spectrum)\ I just want lines which did not begin with brackets () in second column I tried: awk -v var='^(.*' '{if ($2!=var) print $0}' file but it returns whole file, thanks a lot (8 Replies)
Discussion started by: wakatana
8 Replies

10. UNIX for Dummies Questions & Answers

two files.say a and b.both have long columns.i wanna match the column fron 1st file w

ex: a file has : 122323 123456456 125656879 678989965t635 234323432 b has : this is finance no. this is phone no this is extn ajkdgag idjsidj i want the o/p as: 122323 his is finance no. 123456456 this is phone no 123456456 ... (4 Replies)
Discussion started by: TRUPTI
4 Replies
Login or Register to Ask a Question