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
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 fromawk
- 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
to8ドル == theta
. - Variables in
awk
are initialized to blank. This is treated as 0 in mathematical contexts, sofound++
setsfound
to 1 the first time it is executed. I deliberately saidfound++
instead offound = 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 theEND
block to report an error iffound
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 theEND
block and have the script just testawk
's exit status and issue its own error message. You should also do this if you want to capture the output from theawk
(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 theexit
statement from theEND
block.
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
-
I would like to thank every one who replied to my question, it was very helpfuluser156999– user1569992016年03月01日 23:04:48 +00:00Commented Mar 1, 2016 at 23:04
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.
-
With that script, the command
./script1 space input1.txt
will outputspace
, whereas the desired output is1.2
(i.e., theθ2
value from theθ=space
line).G-Man Says 'Reinstate Monica'– G-Man Says 'Reinstate Monica'2016年02月18日 20:48:45 +00:00Commented Feb 18, 2016 at 20:48