Reputation: 511
Hi I'm trying to take the column 9 results corresponding to the first point in column 2 that starts with 4.4. Here's what I have so far.
echo $(awk "$2 ~ /4.4*/ {print $9}" 'myfile') >>"myoutputfile"
Unfortunately this is syntactically incorrect but I can't see how to fix it. Any help would be much appreciated. Thanks.
Upvotes: 0
Views: 119
Reputation: 85785
The correct syntax is:
awk '$2~/^4\.4/{print $9}' myfile >> myoutputfile
This will append the ninth field in myfile
to myoutputfile
if the second fields matches the regex ^4\.4
. The regexp matches if the second field starts with ^
a 4
followed by a period .
followed by a 4
.
Notes:
>>
is the append operator if you only want myoutputfile
to contain the output from the awk
command the use the redirection operator >
this will empty the file first.
awk
prints to stdout by default, using backticks and echo
isn't required.
awk
script should be inclosed in single quotes only and the files do not need quoted here.
EDIT:
To only return the first match using exit
to quit the script:
awk '$2~/^4\.4/{print $9;exit}' myfile
Upvotes: 5
Reputation: 195039
starts with 4.4
should be /^4\.4.*/
so:
awk '$2~/^4\.4.*/{print $9}' myfile >> myoutputfile
some example/explanation:
4.4* matches foo4x4, foo4x4444 ...
^4\.4* matches 4.4, 4.44444, also 4. ... but not 4.45850
^4\.4.* (same as ^4\.4) matches 4.4, 4.4444, 4.45567 and 4.4abcde
If you only want to have numbers following 4.4
, use:
/^4\.4[0-9]*$/
Upvotes: 1
Reputation: 54551
awk '$2 ~ /^4[.]4/ {print $9}' myfile >> myoutputfile
symbol ^
means the beginning of the column.
Upvotes: 2