select rows based on specific column values

linux_lover

How can I select all rows in a text file based on different values of a particular column? The desired values are not in a specific interval.

A short example of what I expect is given below.

My data stored in a text file input.txt

1.0     21
2.5     23
4.0     21
6.5     45
9.0     30
16.5    46
2.0     78
1.0     20
1.6     19
2.5     25
4.0     40
5.2     16
6.5     25
9.0     47
10.0    14
16.5    60
18.5    65
20.0    57
.
.etc

I want to select rows based on 1st column having values 2.5,6.5,16.5,..., etc

So the output will look like this. I don't want to sort output numerically by 1st column.

2.5     23
6.5     45
16.5    46
2.5     25
6.5     25
16.5    60

I am looking for solutions in awk or sed or paste

Thanks in advance.

terdon

You could simply string the numbers together as an awk condition:

$ awk '$1==2.5 || $1==6.5 || $1==16.5' file
2.5     23
6.5     45
16.5    46
2.5     25
6.5     25
16.5    60

That can get really annoying if you have a long list, of course. Another option is to save the numbers you want in another file:

$ cat wantedNums
16.5
2.5 
6.5 

And then use that to filter your input file:

$ awk 'NR==FNR{a[$1]++; next} $1 in a' wantedNums file
2.5     23
6.5     45
16.5    46
2.5     25
6.5     25
16.5    60

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Select rows based on value in specific column

Sum of all rows based on specific column values

Extract the rows based on the specific values in the column by time

duplicate specific rows of a dataframe based on column values

xtensor: Select rows with specific column values

How to Select Rows Based on Column Values in Pandas

Select rows of dataframe based on column values

pandas select rows based on two column values

Select rows of numpy array based on column values

Remove duplicate rows based on previous rows' values in a specific column

Select rows based on group_by rows and its column values

Populating the column values from other rows based on a specific column value

Pandas apply a function to specific rows in a column based on values in a separate column

Select rows with the same value in one column and specific values in another column

Select rows by column value based on range of values in another column in R

Pandas select rows based on randomly selected group from a specific column

How to select rows based on two column that must contain specific value?

Select specific rows based on previous row value (in the same column)

Select rows in one file based on specific values in the second file

Python: Splitting a Column into concatenated rows based on specific Values

Apps Script | How to Archive Specific Rows Based On Values In Column

How to delete rows based on some specific values of a dataframe column?

Combining rows on a Dataframe based on a specific column value and add other values

Select rows with unique values for one specific column in large table

Select rows such that specific column contains values from a list

SQL query to select rows where a column contains only specific values

Select rows from dataframe based on a unique values of other column?

How to select rows in a dataframe based on an "or" operator of values of a column

pandas select rows and cell values based on column and other conditions