Skip to content
Advertisement

Bash – Remove line based on length of first word

I need to delete lines (in a text file) that start with a word that is less than 4 characters. In the example The quick brown fox gets removed since the first word is only three characters in length.

Before:

The quick brown
quick brown fox
brown fox quick

After:

quick brown fox
brown fox quick

Advertisement

Answer

preprocess.awk

#!/usr/bin/awk -f

{                        
  if(length($1) >= 4)    
  {                      
    printf $0"n";       
  }                      
}

output

$ ./preprocess.awk input.txt 
quick brown fox
brown fox quick
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement