Help needed with an awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed with an awk command
# 1  
Old 05-24-2018
Help needed with an awk command

I have a file: tmp_file
(contents of tmp_file)
Code:
DEVICE_111 vss vss dss lsv w 1.16e-07 l 2e-08 la 0 m 1 param 1 ng 1 lp 0 f 1 adcc 1.296e-14 as 1.296e-14
DEVICE_112 vss dss dss lsv w 1.22e-07 l 2e-08 la 0 m 1 param 1 ng 1 lp 0 f 1 adcc 1.296e-14 as 1.296e-14
DEVICE_113 vss dss vss ccv w 1.86e-07 l 2e-08 la 0 m 1 param 1 ng 1 lp 0 f 1 adcc 1.296e-14 as 1.296e-14
DEVICE_114 dss dss dss lvc w 1.2e-07 l 2e-08 la 0 m 1 param 1 ng 1 lp 0 f 1 accd 1.296e-14 as 1.296e-14
DEVICE_115 vss vss dss cvw w 1.55e-07 l 2e-08 la 0 m 1 param 1 ng 1 lp 0nf 1 ccad 1.296e-14 as 1.296e-14

I am using grep+awk command to verify and list certain parameters:
command used:
Code:
grep -n -i "w" tmp_file | awk -v key=w -v num=0.0 -v 'BEGIN {IGNORECASE = 1} {for(i=2;i<=NF;i++){if ($i==key  && $(i+1) ~ /^[0-9.e+e-]+$/ && sprintf("%.30f",$(i+1)) > sprintf("%.30f",num)) print $1":"$i"="$(i+1)":"$(i+2)"="$(i+3)":"$(i+6)"="$(i+7)}}'

Output:
Code:
1:DEVICE_111:w=1.16e-07:l=2e-08:m=1
2:DEVICE_112:w=1.22e-07:l=2e-08:m=1
3:DEVICE_113:w=1.86e-07:l=2e-08:m=1
4:DEVICE_114:w=1.2e-07:l=2e-08:m=1
5:DEVICE_115:w=1.55e-07:l=2e-08:m=1

New Requirement:
I want to multiply the values of w, l and m, which are exponential values
Code:
$(i+1) * $(i+3) * $(i+7)

How to append this command for the new requirement??




Moderator's Comments:
Mod Comment Please use CODE tags for data as well as required by forum rules!

Last edited by Don Cragun; 05-24-2018 at 04:25 AM.. Reason: Added CODE tags. NOPARSE tags added to avoid smiley processing.
# 2  
Old 05-24-2018
Try
Code:
awk -vKEYARR="w m l"  '
BEGIN   {for (n=split (KEYARR, TMP); n; n--) KEYS[TMP[n]]
        }
        {RES = 1
         for (i=1; i<=NF; i++) if ($i in KEYS) RES*=$(i+1)
         print RES
        }
' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-25-2018
Thanks a lot RudiC ... the code worked ....

---------- Post updated at 06:47 AM ---------- Previous update was at 06:46 AM ----------

I used
Code:
 awk -vKEYARR="w m l"  ' BEGIN   {for (n=split (KEYARR, TMP); n; n--) KEYS[TMP[n]] } {RES = 1; for (i=1; i<=NF; i++) if ($i in KEYS) RES*=$(i+1); print RES}' tmp_file

---------- Post updated at 06:47 AM ---------- Previous update was at 06:47 AM ----------

had put it in a single line ....

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - help needed please

Hi.. have a file as below, appreciate if someone can help on this 143|500| 10| 23353 22131 23355 23354 23358 23352 23357 23350 23349 23351| RAID5 213|1008| 9| 22419 22412 221 22413 22414 22416 22417 22415 22418| RAID6 1088|500| 5| 22243 22240 22244 22242 22241| RAID5 322|1200| 12|... (6 Replies)
Discussion started by: richard0@rediff
6 Replies

2. Shell Programming and Scripting

awk help needed

Hi Everyone, i have following in my file 1 2 3 4 5 6 . . 100 and now i want the output as 1 4 7 ..........so on..............97 100 (10 Replies)
Discussion started by: zozoo
10 Replies

3. Shell Programming and Scripting

Help needed with file output awk sed command - please

Hi I have a file that contains lines starting with a particular string plus a Colon: I need to output all these lines but only what comes after the colon Can you pelase assist? Example of lines in the file: com.ubs.f35.cashequities/cashequities: 1 2 ... (5 Replies)
Discussion started by: mnassiri
5 Replies

4. Shell Programming and Scripting

awk help needed

Hi Experts, I have a file (file 1) with several columns and I need to create 2 files based on the data of 20th column of file 1. Criteria 1 : If the 20th field of file1 is empty , copy the entire records to file 2. I am successfully able to do this with the following awk code : awk... (2 Replies)
Discussion started by: nua7
2 Replies

5. UNIX for Dummies Questions & Answers

help needed for awk

Dear all, I am new to use unix. I run the following command and got the error. Anyone knows how should I modify the command. Thanks a lot! $ for chr in 'seq 1 23'; do awk 'BEGIN {print "T","pheno";}{print "M",$2}' out_${chr}.map > dat_${chr}.dat; done error message:... (2 Replies)
Discussion started by: forevertl
2 Replies

6. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

7. UNIX for Dummies Questions & Answers

Awk help needed

I have a log file monitor script that checks through a log file for a string. I use awk to search the log file, starting at the last checked line, for the specified string and then output the count and the last row number checked. The part of the script that does all the work is here: set --... (6 Replies)
Discussion started by: mglenney
6 Replies

8. Shell Programming and Scripting

help needed in awk

Hi , i have a file a.txt like this: far near veryfar toonear typeset var1=veryfar to extract the text between two strings i use the following command : awk '/far/,$veryfar/' a.txt its not working can nyone tell pls whats wrong in it ? i doubt can we use variable in awk like this... (3 Replies)
Discussion started by: santosh1234
3 Replies

9. UNIX for Advanced & Expert Users

help needed on awk

Hi i have a file /services which has the following content. ff1 2201 f1 2202 In my program following logic is used. SVC=$NICKNAME PRT=$(awk '/'$SVC'/ { print substr($2,1,index($2,"/")-1); }'/services) where SVC will be ff1 and f1. when SVC is f1 the output of the... (2 Replies)
Discussion started by: blooddy
2 Replies

10. Shell Programming and Scripting

awk help needed.

Dear All, I want to compare column 3 and column 4 of files(File1 and File2) and want to get the matched records in one file and rejected records in another file.I have tried an awk script,but as Iam not good in it,I cant able to continue.Can anyone help me in this one. File 1... (0 Replies)
Discussion started by: cskumar
0 Replies
Login or Register to Ask a Question