user11897100
user11897100

Reputation:

Limit string length with SED in bash

I need delete words that do not meet string size conditions, in this case, that the string has more than 5 characters but less than 10.

I tried to

sed -ni '/{$carac1,$carac2}$/p' $1

where carac1 is 5 and carac2 is 10, but this didn't function.

Input:

asdasd
aswq
asfasfasgga
sgasgaga
wwqwe

output:

asdasd
sgasgaga
wwqwe

Upvotes: 0

Views: 1571

Answers (1)

Barmar
Barmar

Reputation: 781096

First, you need to use double quotes. Variables aren't expanded inside single quotes.

Second, you need to put something before {min,max}, to specify what should be matched that many times. Use . to match any character.

Third, you need to anchor it at the beginning of the line.

sed -rni "/^.{$carac1,$carac2}$/p" "$1"

You should also quote $1 in case it contains spaces or wildcard characters.

Upvotes: 2

Related Questions