开发者

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

开发者 https://www.devze.com 2023-01-30 12:39 出处:网络
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 categor

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 开发者_JAVA百科separate only those rows which have values 1, 2 3 or 4 in the 8th column. The file is space delimited.


You can use awk as:

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


Yet another AWK answer:

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


Use awk:

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消