Delete words greater than a specific length

HI All,

I have a file with contents like this:

apple
computer
terminal
applecomputernetworkrouterterminalrouter
network
router
applecomputernetworkrouterterminalrouter

I want to remove all lines with length greater than "18 alphabets". Hence, my output should be:

apple
computer
terminal
network
router

This is what I have tried:

awk 'length($0)>=18 filename'

I could find the length of the string but could not delete the lines.

I am using Linux with BASH.

Those are not restricted to alpha characters though ...

grep -v '.\{19\}' infile
sed '/.\{19\}/d' infile

With awk*:

awk 'length <= 18' infile
  • Using the length function without arguments is not portable.
1 Like