awk - Removing extra character when setting variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Removing extra character when setting variable
# 1  
Old 06-11-2015
awk - Removing extra character when setting variable

I have a data file d0 that looks like this:

Code:
$cat d0
server1       running     -n-cv-  8G       3.1%  1435d 15h
server2       running     -n----  8G        39%  660d 22h
server3       running     -n--v-  8G       2.5%  1173d 6h
server4       running     -n----  8G       1.1%  1048d 20h
server5       running     -n----  8G        13%  661d 2h
server6       running     -n----  8G       1.1%  147d 19h
server7       running     -n----  4G       0.7%  933d 15h

I want to take the 4th column and add the results. I have taken a awk statement I used in another script, but the column just had the numbers listed with nothing else:

Code:
$cat d0 | awk '{print $4}' |  awk  '{MEM=$4; TMEM=TMEM+MEM; {printf ("%10d\n",TMEM)}}'
         0
         0
         0
         0
         0
         0
         0

I can get around it by doing the following:

Code:
$cat d0 | awk '{print $4}' |  awk -FG '{MEM=$1; TMEM=TMEM+MEM; {printf ("%10d\n",TMEM)}}'
         8
        16
        24
        32
        40
        48
        52

I am guessing there is an easier way to do it. I know I can use the sed command to removed it. But I am not sure how I would in corporate it.

Code:
$cat d0 | awk '{print $4}' |  sed 's/.$//'
8
8
8
8
8
8
4


Last edited by Scrutinizer; 06-11-2015 at 01:35 PM.. Reason: CODE tags
# 2  
Old 06-11-2015
Assuming that the 4th column reports the memory only in G:

Code:
awk '{split($4, m, "G"); mem=mem+m[1]} END{printf "%10d\n", mem}' d0

# 3  
Old 06-11-2015
What version of awk are you using on what OS. I cannot reproduce your findings:
Code:
awk  '{MEM=$4; TMEM=TMEM+MEM; {printf ("%10d\n",TMEM)}}' file
         8
        16
        24
        32
        40
        48
        52

Code:
awk '{t+=$4} END{printf "%10d\n",t}' file
        52

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed removing extra character from end

Hi, Searching through forum I found "sed 's/*$//'" can be used to remove trailing whitespaces and tabs from file. The command works fine but I see minor issue as below. Can you please suggest if I am doing something wrong here. $ cat a.txt upg_prod_test upg_prod_new $ cat a.txt |sed... (11 Replies)
Discussion started by: bhupinder08
11 Replies

2. UNIX for Dummies Questions & Answers

awk for removing special characters and extra commas

Hi, I have a .csv file which as empty lines with comma and some special characters in 3rd column as below. Source data 1,2,3,4,%#,6 ,,,,,, 1,2,3,4,5,6 Target Data 1,2,3,4,5,6I need to remove blank lines and special charcters I am trying to get this using the below awk awk -F","... (2 Replies)
Discussion started by: shruthidwh
2 Replies

3. Shell Programming and Scripting

setting a shell script variable in awk

The following is part of a larger shell script grep -v "Col1" my_test.log | grep -v "-" | awk '$5 == "Y" {print $1}' instead of printing, can I set set $1 to a variable that the rest of the shell script can read? if $5 == Y, I want to call another shell script and pass $1 as a... (2 Replies)
Discussion started by: guessingo
2 Replies

4. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

5. Shell Programming and Scripting

Awk while-loop printing extra character

Hi, I'm using a while-loop in an awk script. If it matches a regular expression, it prints a line. Unfortunately, each line that is printed in this loop is followed by an extra character, "1". While-statement extracted from my script: getline temp; while (temp ~ /.* x .*/) print temp... (3 Replies)
Discussion started by: redbluefish
3 Replies

6. Shell Programming and Scripting

awk - setting fs to equal any single character

Hi Does anyone know how to set any character as the field separator with awk/nawk on a solaris 10 box. I have tried using /./ regex but this doesnt work either and im out of ideas. thanks (7 Replies)
Discussion started by: chronics
7 Replies

7. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

8. Shell Programming and Scripting

setting a variable, using SSH and awk?

hi there I am trying to get a value from a remote machine into a local variable. To get this value i want to use awk but im having trouble getting it to run, am i escaping in the right places here and using the right quotes (i must have tried a million combinations :() # VAR=`ssh server1... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

9. UNIX for Dummies Questions & Answers

setting environment variable in awk

Dear all, I have a data sample... Dose: Summed ROI: Bladder ************************** Bin Dose Volume 001 0.700 100.000 002 0.715 99.998 168 3.142 0.368 169 3.157 0.338 170 3.171 0.292 Dose: Summed ROI:... (2 Replies)
Discussion started by: tintin72
2 Replies

10. Shell Programming and Scripting

Removing character ' from a variable

Hello there, I have a variable in the form of '/example/file.txt' . I want to remove the ' characters from the beginning and the end so that the my new variable becomes /example/file.txt . How can I do it in a script? I know this is a fairly easy question, but i wasn't able to implement it. (3 Replies)
Discussion started by: sertansenturk
3 Replies
Login or Register to Ask a Question