Remove letter from $1 using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove letter from $1 using awk or sed
# 8  
Old 10-25-2011
If you want to sum B/K/M/G/T correctly, you could use something like this:

Code:
awk 'END {
  printf "%.2fGiB\n", t/1024/1024/1024
  }
{ t += conv($1) }  
func conv(s) {
  # convert to bytes
  s ~ /K$/ && s *= 1024
  s ~ /M$/ && s *= 1024 * 1024
  s ~ /G$/ && s *= 1024 * 1024 * 1024
  s ~ /T$/ && s *= 1024 * 1024 * 1024 *1024
  return s
  }' infile

On Solaris use nawk or /usr/xpg4/bin/awk.

If you have GNU awk with the switch statement enabled:

Code:
awk 'END {
  printf "%.2fGiB\n", t/1024/1024/1024
  }
{ t += conv($1) }  
func conv(s) {
  # convert to bytes
  switch (s) {
    case /K$/: s *= 1024;                      break
    case /M$/: s *= 1024 * 1024;               break
    case /G$/: s *= 1024 * 1024 * 1024;        break
    case /T$/: s *= 1024 * 1024 * 1024 * 1024; break
    }
  return s
  }' infile


Last edited by radoulov; 10-25-2011 at 03:53 PM..
These 2 Users Gave Thanks to radoulov For This Post:
# 9  
Old 10-25-2011
vgersh99,

I tested and got exactly what I was looking for I ended up using:

Code:
tail -1 tSpace | awk '$1+=0' > tess_awk_tSpace

This is the output:

Code:
575 /local/mis/SYBDUMP

nawk is not valid in my environment, thank all of you for your quick response and I will test all suggestions given here to see how they work. Thanks again.

Last edited by radoulov; 10-25-2011 at 04:06 PM.. Reason: Code tags!
# 10  
Old 10-25-2011
It looks as if your file was the result of something like

Code:
du -sh $SomeDir

or
Code:
du -sh *

If you have the control on how this file is produced, then you can use the k option instead of the h option so you can ensure that your result is always displayed in Kb rather than the h ('human') option that can display various units depending on the size of the checked directory.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove repeated letter words

Hi, I have this text file with these words and I need help with removing words with repeated letter from these lines. 1 ama 5 bib 29 bob 2 bub 5 civic 2 dad 10 deed 1 denned 335 did 1 eeee 1 eeeee 2 eke 8... (4 Replies)
Discussion started by: crepe6
4 Replies

2. Shell Programming and Scripting

Want to remove / and character using awk or sed

Below i am trying to remove "/" and "r" from the output, so i need output as: hdiskpower3 hdisk0 hdisk1 #inq | grep 5773 | awk '{print $1}' | sed 's/dev//g' | awk -F"/" '{$1=$1}1' .....................................................//rhdiskpower0 //rhdiskpower1 //rhdiskpower2... (3 Replies)
Discussion started by: aix_admin_007
3 Replies

3. Shell Programming and Scripting

sed awk to remove the , in a string

Dear All, Can anyone help to remove the , bewteen "" in a string by using sed or awk? e.g. input : 1,4,5,"abcdef","we,are,here",4,"help hep" output:1,4,5,"abcdef","wearehere",4,"help hep" Thanks, Mimi (5 Replies)
Discussion started by: mimilaw
5 Replies

4. Shell Programming and Scripting

Awk/sed - add space between dot and letter

I need to change . into . so that e.g. A.Jbecomes A. JI have tried sed 's/\./\.\ /g' but that didn't work. (9 Replies)
Discussion started by: locoroco
9 Replies

5. Shell Programming and Scripting

Remove apostrophe and a letter followed by it

Hi All, For example, I have some words like these: cabinet's school's field's you'll I know how to remove apostrophe from these words using this command: cat filename | tr -s "\'" ' ' But I am not sure how I can remove the letter followed by the apostrophe, so that my output... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

How to remove spaces using awk,sed,perl?

Input: 3456 565 656 878 235 8 4 8787 3 7 35 878 Expected output: 3456 565 656 878 235 8 4 8787 3 7 35 878 How can i do this with awk,sed and perl? (10 Replies)
Discussion started by: cola
10 Replies

7. Shell Programming and Scripting

sed/awk remove newline

Hi, I have input file contains sql queries i need to eliminate newlines from it. when i open it vi text editor and runs :%s/'\n/'/g it provides required result. but when i run sed command from shell prompt it doesn't impact outfile is still same as inputfile. shell] sed -e... (6 Replies)
Discussion started by: mirfan
6 Replies

8. Shell Programming and Scripting

How to remove lines before and after with awk / sed ?

Hi guys, I need to remove the pattern (ID=180), one line before and four lines after. Thanks. (5 Replies)
Discussion started by: ashimada
5 Replies

9. Shell Programming and Scripting

a SED/AWK way to remove everything except...

Hi all, I have a logfile which has lines as following: DOMAIN\username,Deposit,DOMAIN\ServiceAccountName,25/03/2010,00:10,\\SERVER,,,,/Ts=4BAA9BD6,,,10.00,10.03 It's a log of a pcounter print charge system. I need to only have the first part (domain\username) and the second last... (4 Replies)
Discussion started by: necron
4 Replies

10. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies
Login or Register to Ask a Question