0

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    With GNU grep: `grep -E "^.{$carac1,$carac2}$" file` – Cyrus Aug 07 '19 at 17:43
  • 1
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Aug 07 '19 at 17:45
  • This delete all the file –  Aug 07 '19 at 18:14
  • Why is `wwqwe` in the output? Is doesn't have more than 5 characters. Or you mean "at least" and "at most", instead of "more than" and "less than"? – Benjamin W. Aug 07 '19 at 18:17
  • I think that 5 is the minimum length of characters and 10 the maximum, including both. It is somewhat less that, I can fix it easier, but the problem is the command that deletes the entire file. –  Aug 07 '19 at 18:20
  • 1
    My question is: is it "more/less than" or "more/less than or equal"? You write the first, but your attempt seems to imply the second. – Benjamin W. Aug 07 '19 at 18:21
  • 1
    Use `dos2unix < file | grep -E "^.{$carac1,$carac2}$"` to fix line breaks in your file. – Cyrus Aug 07 '19 at 18:30

1 Answers1

2

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Using sed -ni "^.{$carac1,$carac2}$/p" "$1" bash display an error, but if i use sed -ni "/^.{$carac1,$carac2}$/p" "$1" the command delete all the registres of the file –  Aug 07 '19 at 18:01
  • You need to use the `-r` option to use an extended regular expression, or escape the `{` – Barmar Aug 07 '19 at 19:17