Zoltan Ersek
Zoltan Ersek

Reputation: 775

Display lines that have an odd number of characters using grep

Using grep display the lines that have an odd number of characters . Any ideas, I don't know how to get the number of characters from each line, I tried with wc but it only counts the number of characters globally.

Upvotes: 2

Views: 3857

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85873

Get the lines with odd number of characters only using grep:

egrep -v '^(..)+$' file

The normal thing to do would be to use the modulus operator like so:

awk 'length%2' file

Upvotes: 4

choroba
choroba

Reputation: 242198

You can use repetition to find out:

grep -v '^\(..\)\+$' file

Upvotes: 2

Related Questions