Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AWK to filter out column with numerical ranges

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?

like image 568
BurN135 Avatar asked Jan 04 '12 21:01

BurN135


People also ask

What is awk '{ print $1 }'?

If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.

Which awk variable is used to find out the number of columns in a text file?

The following `awk` command will print the first three columns of marks. txt. The for loop is used to print the column values, and the loop includes three steps. The NF variable indicates the total numbers of fields or columns of the file.


1 Answers

awk '{ if ($4 >= 1 && $4 <= 10) print $1 }' sample.txt 
like image 131
Kambus Avatar answered Oct 11 '22 14:10

Kambus