Extract info and do algebra on it by sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract info and do algebra on it by sed or awk
# 15  
Old 02-04-2013
Here is the explanation of what the awk program is doing:
Code:
awk -F, ' NR>1 && $1=="a" {                                             # If current record no > 1 (for skipping header) and 1st field == "a"
  f=1;                                                                  # Set flag variable f = 1
  split($0,a,",");                                                      # Split current records using field separator: , and store in array var: a
} f==1&&$1=="b" {                                                       # If flag variable f == 1 and first field == "b"
  print sqrt(((a[4]+$4)^2)+((a[5]+$5)^2)+((a[6]+$6)^2)+((a[7]+$7)^2));  # Do arithmetic op using array values stored earlier & current record values
  f=0;                                                                  # Reset flag variable back to f = 0
}' filename

This User Gave Thanks to Yoda For This Post:
# 16  
Old 02-04-2013
You can make awk readable too:

Code:
awk -F, '
$2 == 6{
    split($0,a,",")
    getline
    if ($2 == -6) {
       print sqrt(    \
        ((a[8] +$8 )^2) + \
        ((a[9] +$9 )^2) + \
        ((a[10]+$10)^2) + \
        ((a[11]+$11)^2))
    }
}' infile

This User Gave Thanks to Chubler_XL For This Post:
# 17  
Old 02-04-2013
Thanks Bipi and Chubler,
both are quite readable and nice. Smilie

---------- Post updated at 03:47 PM ---------- Previous update was at 03:36 PM ----------

Ok the last question;
how could I append the result in an existing file as another column?
# 18  
Old 02-04-2013
Quote:
Originally Posted by hayreter
how could I append the result in an existing file as another column?
How about using paste command to merge lines of awk o/p and other file.

Give this a try and if it does not work post sample content of other file and show us how you want to merge.
# 19  
Old 02-04-2013
paste actually worked Smilie
I am kind of embarrassed for asking such a stupid question.

Anyway, thanks for all your help guys.
# 20  
Old 02-04-2013
Quote:
Originally Posted by Chubler_XL
The shell is quite capable of reading the required values and matching the correct lines, however, it is only capable of integer arithmetic so you will still need to call awk or bc to do the calculations.

This makes an awk script a much more efficient solution as the shell would be loading and executing an external program for every calculation.
I would still use awk for this, but recent version of the Korn shell (ksh) do recognize exponential constants and are perfectly capable of performing floating point calculations (including a sqrt() function) and printing floating point results.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk to extract additional info

Hi I use multipath linux command to get LUNs info and find out if any failed. # multipath -ll >/tmp/mpfail # cat /tmp/mpfail multipath.conf line 109, invalid keyword: user_friendly_names multipath.conf line 153, invalid keyword: user_friendly_names multipath.conf line 193, invalid... (4 Replies)
Discussion started by: prvnrk
4 Replies

2. Shell Programming and Scripting

Awk/sed HTML extract

I'm extracting text between table tags in HTML <th><a href="/wiki/Buick_LeSabre" title="Buick LeSabre">Buick LeSabre</a></th> using this: awk -F "</*th>" '/<\/*th>/ {print $2}' auto2 > auto3 then this (text between a href): sed -e 's/\(<*>\)//g' auto3 > auto4 How to shorten this into one... (8 Replies)
Discussion started by: p1ne
8 Replies

3. Shell Programming and Scripting

Extract a substring using SED/AWK

Hi All, I have a log file in which name and version of applications are coming in the following format name It may look like following, based on the name of the application and version: XYZ OR xyz OR XyZ OR xyz I want to separate out the name and version and store them into variables.... (4 Replies)
Discussion started by: bhaskar_m
4 Replies

4. Shell Programming and Scripting

Extract word from text (sed,awk, etc...)

Hello, I need some help extracting the number after the RBA e.g 15911688 from the below block of text (e.g: grep RBA |sed .......). The code should be valid for blocks if text generated at different times as well and not for the below text only. ... (2 Replies)
Discussion started by: drbiloukos
2 Replies

5. Shell Programming and Scripting

Using AWK BEGIN to extract file header info into variables

Hi Folks, I've searched for this for quite a while, but can't find any solution - hope someone can help. I have various files with standard headers. eg. <HEADER> IP: 1.2.3.4 Username: Joe Time: 12:00:00 Date: 23/05/2010 </HEADER> This is a test and this part can be any size... (6 Replies)
Discussion started by: damoske
6 Replies

6. UNIX for Dummies Questions & Answers

Using awk/sed to extract text between Strings

Dear Unix Gurus, I've got a data file with a few hundred lines (see truncated sample)... BEGIN_SCAN1 TASK_NAME=LA48 PDD Profiles PROGRAM=ArrayScan 1.00 21.220E+00 2.00 21.280E+00 END_DATA END_SCAN1 BEGIN_SCAN2 TASK_NAME=LA48 PDD Profiles 194.00 2.1870E+00 ... (5 Replies)
Discussion started by: tintin72
5 Replies

7. Shell Programming and Scripting

[sed/awk] find info and replace

Hi :) I have some problems with "FOR"... I have a text file in this format: name1 www.link1/random_number name2 www.link2/random_number name3 www.link3/random_number ... (Names and info changes) Now, I need: (4 Replies)
Discussion started by: aspire
4 Replies

8. Shell Programming and Scripting

how to extract info from a file using awk

Dear all I have a file call interfaces.txt Filename: interfaces.txt How can I extract the information at below? ABC_DB_001 hostname1 20901 ABC_DB_002 hostname2 20903 ABC_DB_003 hostname3 20905 Currently I am using a very stupid method grep ^ABC interfaces.txt > name.txt grep... (3 Replies)
Discussion started by: on9west
3 Replies

9. Shell Programming and Scripting

Extract some characters with SED or AWK

Hi, I have the following example string: today_is_a_good_day.txt The character "_" inside the string can sometimes be more or less. The solution for every string equal the count of "_" should be alway the rest after the last underline character. Result: day.txt I want to use awk... (5 Replies)
Discussion started by: climber
5 Replies

10. Shell Programming and Scripting

extract using sed/awk - need help? Please!!

Need help..not sure how to use with awk or sed I want to take data from the notification.$$ file and assign the data to variable "group". Not sure how to do it. The data I want to extract from the notification.$$ is on the first line of the file ..right after the (notice): NetWorker... (5 Replies)
Discussion started by: gzs553
5 Replies
Login or Register to Ask a Question