How to evaluate a string of numbers in the same command of AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to evaluate a string of numbers in the same command of AWK
# 1  
Old 09-19-2011
How to evaluate a string of numbers in the same command of AWK

Hi,
I am trying to do evaluate one numerical string after substitution.
++++++++++++++++==
What I have = "7.04+2.3Xlog(0.72e-6X1.0e6)X1.9596"
What I need = evaluate 7.04+2.3*log(0.72e-6*1.0e6)*1.9596 = 5.55941
what I am doing;
Code:
echo "7.04+2.3Xlog(0.72e-6X1.0e6)X1.9596" | awk '{kk=gensub(/X/,"*","g"); ak=(kk); {print ak};exit}'

here intention is to replace all "X" with "*" and then evaluate the string of numbers and print the evaluated value which I am unable to achieve. Please help

regds
A new member of the Forum(Vivek)

Last edited by Franklin52; 09-19-2011 at 08:45 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 09-19-2011
Code:
$ echo "7.04+2.3Xlog(0.72e-6X1.0e6)X1.9596" | sed -e 'y/Xe/\*\^/' -e 's/log/l/g' | bc -l
15.92356466100994563050

"bc" requires -l for logarithm and the function name is "l"
Exponents are represented by "^"

--ahamed
# 3  
Old 09-20-2011
Thanks

Hi Ahamed,
Thanks a lot, it is working and out of awk complications.
Only thing is which I need to take care is the bc -l is calculating on natural log but I need to use log of base 10.
Thanks a lot for your help
regds
vivek sharma
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to evaluate expression under awk?

I have to display only those subscribers which are in "unconnected state" and the date is 90 days older than today's date. Below command is used for this purpose: cat vfsubscriber_20170817.csv | sed -e 's/^"//' -e '1d' | \ nawk -F '",' '{if ( (substr($11,2,4) == 2017) && ( substr($11,2,8)... (1 Reply)
Discussion started by: dia
1 Replies

2. Shell Programming and Scripting

Evaluate Expression within awk

I want to create a conditional expression string and pass in an awk script. My script is as below... comm="\$3 == "hello"" awk -F "^T" -v command="${comm}" ' { if ( command ) { print "hye" } }' testBut the statement "if ( command )" always evaluates to true which is not... (5 Replies)
Discussion started by: Saikat123
5 Replies

3. Shell Programming and Scripting

awk : match only the pattern string , not letters or numbers after that.

Hi Experts, I am finding difficulty to get exact match: file OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE="" DHCP_ENABLE=0 INTERFACE_NAME="lan3:1"... (6 Replies)
Discussion started by: rveri
6 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

awk if statement to evaluate string and compare

I have the following simplified code that I am planning on putting into a larger shell script. I have been butchering it to try and make work amongst google searches and reading awk documentation. amixer sset Master toggle | awk '{ if ( /^ Front Left/ { print $7 } == // ) print "MUTED" }'I... (2 Replies)
Discussion started by: jelloir
2 Replies

7. Shell Programming and Scripting

A mistake in awk command I used to parse numbers

Hi I have a big file with a certain pattern (shown below) from which I need to parse out some digits in tabular format. The format of the file is: '-' indicates text which doesn't to be parsed # Output of huzzle for sequence file 1000.Clade1.html - - - -- -------... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

8. Shell Programming and Scripting

Evaluate string containing shell variable names

Hello, I have this: #!/usr/bin/ksh V1=ABC str="hello 123;${V1}" eval "echo $str" i get hello 123 /script.sh ABC not found However eval works if $str variable doesn't contain a semicolumn (eg if str="hello 123~${v1}" running the eval statement above would produce (2 Replies)
Discussion started by: endorphin
2 Replies

9. Shell Programming and Scripting

Problem with sub command (awk) and numbers

Hi, I am trying to perform a simple soustraction between two floating numbers and cannot get it done for some reason due to the use of the sub command. The following is the straight-forward result of the soustraction: $ echo | gawk '{a=968;b=967.99;c=a-b;print c}' ... (2 Replies)
Discussion started by: Indalecio
2 Replies

10. Shell Programming and Scripting

awk/sed Command: To Parse Stament between 2 numbers

Hi, I need an awk command that would parse the below expression Input Format 1 'Stmt1 ............................'2 'Stmt2 ............................'3 'Stmt3 ............................'4 'Stmt4 ............................'5 'Stmt5 ............................'6 'Stmt6... (1 Reply)
Discussion started by: rajan_san
1 Replies
Login or Register to Ask a Question