How to extracting rows that have the same values in all columns in Linux ?
2
0
Entering edit mode
2.3 years ago
Ahmad ▴ 10

Hi, every one

I have csv file contains many rows, I need extract all rows that have AB value in all columns this sample from my csv file

18125 OAR20_24441405.1 31641310 20 24441405 0.970312 AB AA AB AA AA AB AB AA AA AB AA AA AA AB AA AA AA AA AA AB AA AA AA AA

58012 ilmnseq_rs406679941 45641375 18 46692891 0.9673617 BB AB BB AB BB BB BB BB AB BB BB BB BB BB BB AB AA BB AB AA BB AB BB AA

30159 OAR6_53858058.1 67675371 6 53858058 0.9672967 BB AA BB AA BB AB AB AB AA AB AB AA AB AA AA AB AB BB AB BB AB AA AA BB

24711 OAR3_55534628.1 52677413 3 55534628 0.9671658 BB BB AB BB BB AB BB AA BB AA AB AB AB BB BB AA AB AB BB BB AA AB AA AB

27127 OAR4_85875873.1 33712385 4 85875873 0.9658307 AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB
csv linux awk • 1.7k views
ADD COMMENT
4
Entering edit mode
2.3 years ago
awk '{for(i=7;i<=NF;i++) if($i!="AB") next; print}' input.txt
ADD COMMENT
0
Entering edit mode

damnit :-) just beat me to it .

I had a slightly different approach :

awk '{for(x=6; x<=NF;x++){if($x ~ /AB/){z++}}} END{if (z == NF-6) print }'

EDIT: yours will be bit more efficient, coming to think of it

ADD REPLY
0
Entering edit mode

Thanks for your trial

ADD REPLY
0
Entering edit mode

Thanks, Pierre

ADD REPLY
0
Entering edit mode

Dear, can I keep the first row containing the column names ?

ADD REPLY
2
Entering edit mode
awk '(NR==1) {print;next } {for... `
ADD REPLY
0
Entering edit mode

Thank you so much

ADD REPLY
0
Entering edit mode
2.3 years ago

One of the easiest ways

cat your_file.csv | grep "AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB AB" > my_selected.file
ADD COMMENT
0
Entering edit mode

Thank you Antonio for your answer

But I need smart command line with one "AB" that repeated in all columns

ADD REPLY
0
Entering edit mode

Don't understand your need.. sorry

ADD REPLY

Login before adding your answer.

Traffic: 1644 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6