41

I'm relatively new to BASH and I'm trying to use awk to filter out column 1 data based on the 4th column of a text file. If the 4th column of data matches the range of x, then it'll output column 1 data. "x" is suppose to be a range of numbers 1-10 (1,2,3..10).

awk -F: '{ if($4=="x") print $1}' filename.txt

filename.txt 
sample1 0 0 4
sample2 0 0 10
sample3 0 0 15
sample4 0 0 20

Actual use:

awk -F: '{ if($4=="1-10") print $1}' sample.txt
output = sample1, sample2, sample3, sample4

It should be: sample1 sample2 only.

Is there is an error in the syntax that I'm not seeing or I could be possibly using this syntax completely wrong?

0

6 Answers 6

75
awk '{ if ($4 >= 1 && $4 <= 10) print $1 }' sample.txt
5
  • 15
    You can use expressions as patterns and do away with the if, like so:awk '$4 <= 10 && $4 >= 1 { print $1 }' file.txt
    – potong
    Jan 4, 2012 at 21:54
  • 3
    Also, awk '1 <= $4 && $4 <= 10' sample.txt works great for people like me that want to output whole lines based on value of one column.
    – arekolek
    Aug 3, 2016 at 20:38
  • @arekolek may I ask what should I do if my 1 and 10 are variables $one and $ten? I tried awk '"'"$one"'" <= $4 && $4 <= "'"$ten"'"' sample.txt , but the less than ten part did not work somehow
    – Helene
    Oct 24, 2017 at 23:12
  • 1
    @Helene, I understand one is a bash variable, so I'd try awk "$one <= \$4 && \$4 <= $ten" so that bash substitutes only $one and $ten in the string passed to awk.
    – arekolek
    Oct 25, 2017 at 7:09
  • 1
    Use -v option to pass variable into awk: awk -v a="$one" -v b="$ten" 'a <= $4 && $4 <= b'
    – ruvim
    Nov 24, 2017 at 10:51
18
awk '$4 ~ /^[1-9]$|^10$/{print $1}' sample.txt

output:

sample1
sample2

explanation:

  • ^[1-9]$ --> $4 must be a single digit from 1 to 9
  • | (the pipe) --> or
  • ^10$ --> $4 must be the number 10
8
awk -F ':' '$4 >= 1 && $4 <= 10{print $1}'
1
  • 1
    already suggested in comments. Read all the answers before answering something very similar, specially if the question is old. Nov 2, 2016 at 20:41
4

There may be a way to do it using only awk (nevermind, see my edit below), but I don't know of it. I'd combine it with grep:

egrep ' ([1-9]|10)$' sample.txt | awk '{print $1}'

I think you are matching the fourth column with the string "1-10" not the range. Also, -F: will change the delimiter to a colon rather than a space.

Edit:

awk '$4 ~ /^([1-9]|10)$/ {print $1}' sample.txt
2

If you want awk to look up values from a range then you can set that range in the BEGIN statement.

awk 'BEGIN{for (i=1;i<=10;i++) a[i]} ($4 in a){print $1}' sample.txt 

Test:

[jaypal:~/Temp] cat sample.txt 
sample1 0 0 4
sample2 0 0 10
sample3 0 0 15
sample4 0 0 20
[jaypal:~/Temp] awk 'BEGIN{for (i=1;i<=10;i++) a[i]} ($4 in a){print $1}' sample.txt 
sample1
sample2
1

If Perl is an option, you can try this solution similar to Kambus's awk solution:

perl -lane 'print $F[0] if $F[3] >= 1 && $F[3] <= 10' sample.txt

These command-line options are used:

  • -n loop around every line of the input file, do not automatically print every line

  • -l removes newlines before processing, and adds them back in afterwards

  • -a autosplit mode – split input lines into the @F array.

  • -e execute the perl code

@F is the array of words in each line, indexed starting with 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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