0

I have a file "input.txt" has variables A1, A2, A3, θ, θ1 and θ2 - sample of input.txt is as follows:

$ cat input1.txt 

A1=5.2 A2=4.9  A3=6.1 θ=space    θ1=2.5 θ2=1.2 

A1=3.1 A2=5.1  A3=3.7 θ=triangle θ1=8.1 θ2=3.9

I want to create a script to run over the file input.txt - this file will be passed as a second argument, the first argument would be the value of θ

I created a script as follows:

#! /bin/bash

file=input1.txt

if grep -q $1 "$file"; 
then
awk -F '[= ]+' '{ print $12 }' <$2

else

echo "Not available"
fi
}

But when I run this script as follows:

./script space input.txt   

(first argument is the value of θ and second argument is the file name), the output is all the values in field 12:

$ ./script1 space input1.txt 
1.2
3.9

the output should be 1.2 only, I searched and found that I need to create a loop to read the file line by line but I can not get it to work.

3 Answers 3

2

You can do all the work in awk:

#!/bin/sh
file=$2

awk -v theta="$1" -F '[= ]+' '
        $0 ~ theta { print $12; found++ }
        END        { if (!found) { print "Not available"; exit 1 } }' "$file"

You might want to add error handling to verify that there are two command-line arguments, and $2 is the name of a readable file, because

  • if $2 is something other than a readable file, you will get an error message from awk
  • if $2 is blank or absent, awk will silently read from the standard input.

(Of course, either or both of these behaviors might be OK with you.)

Notes:

  • You may get more targeted results by changing $0 ~ theta to $8 == theta.
  • Variables in awk are initialized to blank.  This is treated as 0 in mathematical contexts, so found++ sets found to 1 the first time it is executed.  I deliberately said found++ instead of found = 1 so, if multiple lines match the theta value, found will be set to the number of such lines.  This seems like it should be an error condition; if you are concerned about it, you can modify the END block to report an error if found is anything other than 1.
  • Of course, if you need your script to do one thing if a value is found and something else if it isn't, you can delete the print statement from the END block and have the script just test awk's exit status and issue its own error message.  You should also do this if you want to capture the output from the awk (i.e., the θ2 value).  Conversely, if all you need is a human-readable error message, and you don't need to be able to check exit status, you can delete the exit statement from the END block.
0

Try this:

#! /bin/bash

file=input1.txt

if grep -q $1 "$file";
then
        grep $1 $2 | awk -F '[= ]+' '{ print $12 }'
else
        echo "Not available"
fi
1
  • I would like to thank every one who replied to my question, it was very helpful
    – user156999
    Mar 1, 2016 at 23:04
0

It looks like your logic can be boiled down to:

grep -om1 "$1" < "$2" ||
echo 'Not available.'

...provided a GNU grep, anyway. Though, if it were me, I would drop the echo, or at least do...

! echo 'Not available.' >&2

...instead.

1
  • With that script, the command ./script1 space input1.txt will output space, whereas the desired output is 1.2 (i.e., the θ2 value from the θ=space line). Feb 18, 2016 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.