Remove parenthesis and run awk calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove parenthesis and run awk calculation
# 1  
Old 06-02-2016
Remove parenthesis and run awk calculation

The awk below works fine if I manually remove the () from file1. However, when I try to use tr to remove the () and then | into awk to run the calculation no result is obtained. Is there a way to tell the awk to ignore the () in fiile1 if they are there or do I need to remove them first? Thank you Smilie.

awk with tr
Code:
cat file1 | tr -d '()' | awk 'FNR==NR{A[$1]=$2;next} ($1 in A){X=(A[$1]/$3)*100;printf("%s %.2f\n",$1,  100-X)}' file1 file2

file1
Code:
(ADH5) 1125

file2
Code:
ADH5 9 1125

desired output
Code:
ADH5 0.00

# 2  
Old 06-02-2016
You certainly know that all your efforts catting and tring are in vain if your awk doesn't read from stdin i.e. the last pipe?

Try
Code:
awk '{gsub(/[()]/,_)} FNR==NR{A[$1]=$2;next} ($1 in A){X=(A[$1]/$3)*100;printf("%s %.2f\n",$1,  100-X)}' file1 file2
ADH5 0.00

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-02-2016
I am not too familiar with gsub. awk '{gsub(/[()]/,_)}, does this look at all lines of the file and if it finds (), removes them? Thank you Smilie

Just for learning could I specify a field in the gsub? I don't need to in this case but might in the future.

Maybe,
Code:
awk '{gsub $1(/[()]/,_)}

# 4  
Old 06-02-2016
man awk:
Quote:
gsub(r,s,t) gsub(r,s)
Global substitution, every match of regular expression r in variable t is replaced by string s. The number of replacements is returned. If t is omitted,
$0 is used.
t can be any variable including fields.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-02-2016
Quote:
Originally Posted by cmccabe
I am not too familiar with gsub. awk '{gsub(/[()]/,_)}, does this look at all lines of the file and if it finds (), removes them? Thank you Smilie
Just for learning could I specify a field in the gsub? I don't need to in this case but might in the future.
Maybe,
Code:
awk '{gsub(/[()]/,_)}

Hello cmccabe,

As RudiC already stated the use of gsub(/[()]/,_) means substituting /[()]/,_ red colored characters with blue character's value(which is NULL).

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-02-2016 at 03:39 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 06-13-2016
Thank you both very much Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk calculation with zero as N/A

In the below awk, I am trying to calculate percent for a given id. It is very close the problem is when the # being used in the calculation is zero. I am not sure how to code this condition into the awk as it happens frequently. The portion in italics was an attempt but that lead to an error. Thank... (13 Replies)
Discussion started by: cmccabe
13 Replies

2. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. UNIX for Dummies Questions & Answers

Renaming files to remove everything before one parenthesis

Hi, I have files in a folder that I would like all renamed without the preceding number and parenthesis. For example, I have files of the name 08) Great Good Fine Ok - Not Going Home 09) Roosevelt - Small Hours 10) RAC - I Should've Guessed Feat. SPEAK and I would like them all to be... (4 Replies)
Discussion started by: jyu429
4 Replies

4. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

5. Programming

arithmetic calculation using awk

hi there again, i need to do a simple division with my data with a number of rows. i think i wanted to have a simple output like this one: col1 col2 col3 val1 val2 val1/val2 valn valm valn/valm any suggestion is very much appreciated. thanks much. (2 Replies)
Discussion started by: ida1215
2 Replies

6. Shell Programming and Scripting

awk calculation problem

I have a list of coordinate data, sampled below. 54555209 784672723 I want it as: 545552.09 7846727.23 Below is my script: BEGIN {FS= " "; OFS= ","} {print $1*.01,$2*.01} This is my outcome: 5.5e7 7.8e8 How do I tell awk that I want to keep all the digits instead of outputting... (1 Reply)
Discussion started by: ndnkyd
1 Replies

7. Shell Programming and Scripting

Remove parenthesis character (Perl)

Hello, i'm unable to remove the parenthesis character. With $parsed_AsciiName =~ s/\(//; the string is the same And with $parsed_AsciiName =~ s/(//; i retrieve "Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE" Any ideas, please? thank you in advanced. (4 Replies)
Discussion started by: aristegui
4 Replies

8. UNIX for Advanced & Expert Users

Reattemps Calculation using awk

Dear All How are you I have files which look like this : 20080406_12:43:55.779 ISC Sprint- 39 21624032999 218925866728 20080406_12:44:07.811 ISC Sprint- 20 21620241815 218927736810 20080406_12:44:00.485 ISC Sprint- 50 21621910404 218913568053... (0 Replies)
Discussion started by: zanetti321
0 Replies

9. Shell Programming and Scripting

awk calculation

Hallo all, I have a script which creates an output ... see below: root@a7germ:/tmp/pax > cat 20061117.txt 523.047 521.273 521.034 517.367 516.553 517.793 513.114 513.940 I would like to use awk to calculate the (a)total sum of the numbers (b) The average of the numbers. Please... (4 Replies)
Discussion started by: kekanap
4 Replies

10. Shell Programming and Scripting

Need to only remove parenthesis and : leave the rest

Hi all, I'm stuck on this last part...am running a simple script under AIX to extract NetView host IP addresses. The line below returns the IP address in parenthesis with a trailing colon, i.e. ping -c 1 $name |grep \( | awk '{ print $3 }' --------> returns (a.b.c.d): How can I only... (10 Replies)
Discussion started by: livinthedream
10 Replies
Login or Register to Ask a Question