sfactor
sfactor

Reputation: 13062

Bash: how do i choose rows from a file that have particular value in the column

I have a huge file that I need to analyze. What I want to do is separate those rows that have certain values in a certain column. So, its like choosing only those data that belong to a certain category. How can this be accomplished with a simple bash commnand or script.

For example, I want to separate only those rows which have values 1, 2 3 or 4 in the 8th column. The file is space delimited.

Upvotes: 4

Views: 3479

Answers (4)

Dennis Williamson
Dennis Williamson

Reputation: 360005

Yet another AWK answer:

awk '$8 ~ /1|2|3|4/' inputfile

Upvotes: 4

sorpigal
sorpigal

Reputation: 26086

Yet another awk answer.

awk '$8 ~ /[1-4]/' file

But, just for some variety, a bash answer

while read line ; do
    fields=($line)
    [[ ${fields[7]} =~ [1-4] ]] && echo $line
done < file

Upvotes: 2

Benoit
Benoit

Reputation: 79175

Use awk:

awk '$8 >= 1 && $8 <= 4' your_file.txt

Upvotes: 3

codaddict
codaddict

Reputation: 454980

You can use awk as:

awk '$8 == 1 || $8 == 2 || $8 == 3 || $8 == 4' file

Upvotes: 4

Related Questions