Bash: how to select rows from a file with a specific value in a column

I have a huge file that I need to parse. What I want to do is split those rows that have specific values ​​in a specific column. Thus, it is like selecting only data that belongs to a certain category. How can this be done with a simple bash commnand or script.

For example, I want to separate only those rows that have values ​​1, 2, 3, or 4 in the eighth column. The file is limited to spaces.

+3
source share
4 answers

You can use awk like:

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

Use awk:

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

AWK:

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

awk.

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

, , bash

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

Source: https://habr.com/ru/post/1780002/


All Articles