How to search, replace and multiply variable within awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search, replace and multiply variable within awk?
# 1  
Old 07-23-2014
How to search, replace and multiply variable within awk?

I have a file that reports the size of disks GB's or TB's - I need the file to report everything in MB's. Here is an extract of the file - the last column is the disk size.


Code:
19BC 2363 20G
1AA3 2363 2.93T
1A94 2363 750G


Whenever I come across a G I want to delete the G and multiply by 1024 and whenever I get "T" I want to delete the T and multiply by 1048576.

So the output file would look like

Code:
19BC 2363 20480
1AA3 2363 3072327
1A94 2363 768000

I had this working to convert the G but then I saw that there were "T"'s in the file and I am struggling to convert G's and T's...Here is what I was using to deal with the G's

Code:
cat file | sed 's/G$//g' | awk '{MB=$3*1024; print$1,$2,MB}'

# 2  
Old 07-23-2014
Code:
awk '$NF ~ /G/ {$NF *= 1024}1 $NF ~ /T/ {$NF *= 1048576}1' file

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 07-23-2014
Thanks for that. it works great!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

3. UNIX for Dummies Questions & Answers

Search and replace with variable

Hello all, I stumbled upon a command line for multiple search and replace within given destination perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' *.html I want to replace the following line where the date is the variable, from <div class="meta"> <ul> <li>05.05.2015 with date tags, like... (5 Replies)
Discussion started by: uninuub
5 Replies

4. Shell Programming and Scripting

Multiply certain column to variable

Hi experts, I want to multiply certain columns to variable, data : 1 2 3 4 5 6 7 8 9 result with var = 2 for column 3,6,9 ... (every columns which can be divided to 3): 1 2 6 4 5 12 7 8 18 I have tried : awk 'BEGIN{FS=OFS=" "}{print $1,$2,$3*a,$4,$5,$6*a,$7,$8,$9*2 }' a=2 file.txt but how... (6 Replies)
Discussion started by: guns
6 Replies

5. Shell Programming and Scripting

search replace with loop and variable

Hi, could anyone help me with this, tried several times but still not getting it right or having enough grounding to do it outside of javascript: Using awk or sed or bash: need to go through a text file using a for next loop, replacing substrings in the file that consist of a potentially multi... (3 Replies)
Discussion started by: wind
3 Replies

6. Shell Programming and Scripting

search the pattern in a file and replace with variable already defined previously in csh

I want to replace a certain pattern with the variable already defined. e.g. set path_verilog = /home/priya/bin/verilogfile my file contents are : verilog new verilog is defined here verilog_path_comes I am using the below command sed 's/verilog_path_comes/'$path_verilog'/g' <filename>... (2 Replies)
Discussion started by: nehashine
2 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

Perl search and replace in range using variable

Hi. I have a file with asterisk field separators and backslash line terminators. The first field in each line names the line type. I am trying to process each range separately. Here's what the data looks like: BA*DATA\ LS*DATA1*DATA2*00020*\ TA*DATA1*DATA2*DATA3*\ TA*DATA1*DATA2*DATA3*\... (1 Reply)
Discussion started by: yoi2hot4ya
1 Replies

9. Shell Programming and Scripting

multiply variable

I need to multiply the value of a variable and then store it in another variable. I have EXPHOURINSEC=$(($EXPDATEHOUR * 3600)) but i get an error saying the * is unexpected. Im using ksh (4 Replies)
Discussion started by: fwabbly
4 Replies

10. Shell Programming and Scripting

search & replace in variable

Can I use search & replace in any variable? Suppose I have one variable named var1 which holds value "abcabc" I need to search 'a' in var1 and want to replace with 'x' like 'xbcxbc'. Is it possible? Can you provide me an example? Malay (3 Replies)
Discussion started by: malaymaru
3 Replies
Login or Register to Ask a Question