0

How to write this code please with correct syntax? I would like to give a value for a and then write a condition - if first column is equal to a then write this line. Thank you

#!/bin/bash
for i in {0..2}; do
awk -v var=$i 'a==0.065+var*0.005 $1 == a { print $0 } test.res > outa="$a"' 

test.res

   6.500000e-02   1.400000e+02   3.000000e-01   8.195821e+05   6.669776e-01
   6.500000e-02   1.400000e+02   3.500000e-01   7.085306e+05   5.766037e-01
   6.500000e-02   1.400000e+02   4.000000e-01   7.737306e+05   6.296636e-01
   6.500000e-02   1.400000e+02   4.500000e-01   7.929304e+05   6.452884e-01
   7.000000e-02   1.600000e+02   5.000000e-01   1.561877e+06   1.271059e+00
   7.000000e-02   1.600000e+02   5.500000e-01   1.548409e+06   1.260098e+00
   7.000000e-02   1.600000e+02   6.000000e-01   1.644194e+06   1.338048e+00
   7.500000e-02   1.400000e+02   3.000000e-01   6.659456e+05   5.419479e-01
   7.500000e-02   1.400000e+02   3.500000e-01   7.285058e+05   5.928595e-01
   7.500000e-02   1.400000e+02   4.000000e-01   7.265234e+05   5.912463e-01
   7.500000e-02   1.400000e+02   4.500000e-01   2.780260e+06   2.262581e+00
   7.500000e-02   1.400000e+02   5.000000e-01   9.186277e+06   7.475812e+00

Desired output

outa=0.065
   6.500000e-02   1.400000e+02   3.000000e-01   8.195821e+05   6.669776e-01
   6.500000e-02   1.400000e+02   3.500000e-01   7.085306e+05   5.766037e-01
   6.500000e-02   1.400000e+02   4.000000e-01   7.737306e+05   6.296636e-01
   6.500000e-02   1.400000e+02   4.500000e-01   7.929304e+05   6.452884e-01

outa=0.070
   7.000000e-02   1.600000e+02   5.000000e-01   1.561877e+06   1.271059e+00
   7.000000e-02   1.600000e+02   5.500000e-01   1.548409e+06   1.260098e+00
   7.000000e-02   1.600000e+02   6.000000e-01   1.644194e+06   1.338048e+00

outa=0.075
   7.500000e-02   1.400000e+02   3.000000e-01   6.659456e+05   5.419479e-01
   7.500000e-02   1.400000e+02   3.500000e-01   7.285058e+05   5.928595e-01
   7.500000e-02   1.400000e+02   4.000000e-01   7.265234e+05   5.912463e-01
   7.500000e-02   1.400000e+02   4.500000e-01   2.780260e+06   2.262581e+00
   7.500000e-02   1.400000e+02   5.000000e-01   9.186277e+06   7.475812e+00
Lukáš Altman
  • 497
  • 1
  • 5
  • 15

2 Answers2

1

If you like to use value of i from the for loop you need to declare the variable in awk

awk -v var=$i 'a==0.065+var*0.005 .....

If you post samle data and how you like the output to be, I guess we can do all in awk without need for external for loop.

Jotne
  • 40,548
  • 12
  • 51
  • 55
1

== is a comparison, = is an assignment.

$ i=1; awk -v var="$i" 'BEGIN{a=0.065+var*0.005} $1 == a' file
   7.000000e-02   1.600000e+02   5.000000e-01   1.561877e+06   1.271059e+00
   7.000000e-02   1.600000e+02   5.500000e-01   1.548409e+06   1.260098e+00
   7.000000e-02   1.600000e+02   6.000000e-01   1.644194e+06   1.338048e+00

Don't use = in your output file names btw as it makes it hard for subsequent calls to tools like awk to differentiate file names from variable assignments.

awk 'script' outa=1

is calling awk with no file names and a variable named outa with the value 1. You'd need to do:

awk 'script' ./outa=1

to tell awk that outa=1 is a file name.

Finally - it's not clear why you're using a loop at all instead of just:

awk '{print > sprintf("outa_%.03f",$1)}' file

or if you'll have more than 15 or so output files and aren't using gawk to handle all those open files:

awk '{curr=sprintf("outa_%.03f",$1)} curr!=prev{close(prev); prev=curr} {print > curr}' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185