-1

I would like to check a specific pattern / string in a file and if its present in all the columns i want to delete the corresponding line.

Example:

name1   name2   name3   name1   name1   name2   name3
name1   name2   name3   name1   name2   name1   
name2   name1   name1   name2   name3   name1   
name2   name2   name2   name2   name2   

In this file I want to delete row 4 because it contain same name throughout the columns.

I have huge file and number of columns can't be defined manually in awk or grep.

  • Please define "escape/del". – Satō Katsura Nov 22 '16 at 15:38
  • Mean i want to delete that line from the file – new bie Nov 22 '16 at 15:39
2

With awk:

awk 'NF>1 { for(i=2; i<=NF; i++) if($i != $1) { print; next } } NF<=1' file

With GNU sed, or a recent BSD sed:

sed -E '/^[ \t]*([^ \t]+)([ \t]+\1)+[ \t]*$/d' file
1

With perl

$ perl -lane '(@a) = /$F[0]/g; print if $#F != $#a' file 
name1   name2   name3   name1   name1   name2   name3
name1   name2   name3   name1   name2   name1   
name2   name1   name1   name2   name3   name1   
  • Split input line on spaces
  • If number of fields is same as number of matches when first column is used as pattern, do not print that line


With grep if the file always has same amount of spaces after each column:

$ grep -vxE '(\S+\s+)\1*' file 
name1   name2   name3   name1   name1   name2   name3
name1   name2   name3   name1   name2   name1   
name2   name1   name1   name2   name3   name1   


Note: If single column lines should be part of output, use

perl -lane '(@a) = /$F[0]/g; print if $#F != $#a || $#F == 0' file 

and

grep -vxE '(\S+\s+)\1+' file 
  • 1
    Both patterns will delete lines with a single column. – Satō Katsura Nov 22 '16 at 16:04
0

Another way to do it in perl might be to map the fields into a hash and then count the resulting keys by evaluating them in a scalar context e.g.

$ perl -ane '%names = map { $_ => 1 } @F; print unless keys %names == 1' example
name1 name2 name3 name1 name1 name2 name3
name1 name2 name3 name1 name2 name1
name2 name1 name1 name2 name3 name1

or, more compactly, using an anonymous hash

$ perl -ane 'print unless keys %{ {map { $_ => 1 } @F } } == 1' example
name1 name2 name3 name1 name1 name2 name3
name1 name2 name3 name1 name2 name1
name2 name1 name1 name2 name3 name1
  • This will delete lines with a single column. – Satō Katsura Nov 23 '16 at 6:35
  • Would it be possible to not match the whole character of the cell means name2 but just to match certain string like "name" and based on that exclude the row? – new bie Nov 23 '16 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.