-2

I was using this junk character filtering command in RHEL 6.10 which was working perfectly.

sed 's/[^][A-Za-z0-9\^`~!@#$%&*|\,:;{}()+=_-./ "<>?\/\\]//g' 

However, in RHEL 8.3 below error happens.

sed: -e expression #1, char 54: Invalid range end

Any advice is much appreciated

mma
  • 1
  • Does this answer your question? [Hyphen and underscore not compatible in sed](https://stackoverflow.com/questions/42743964/hyphen-and-underscore-not-compatible-in-sed) – Wiktor Stribiżew Oct 20 '21 at 07:15
  • The `-` must be at the start or end of the bracket expression. You also need to remove all escapes except from one `/` (note you have one `/` unescaped). You do not need to double escape ``\`` inside a POSIX bracket expression. – Wiktor Stribiżew Oct 20 '21 at 07:16
  • i've tried that, it will remove the ```-``` from the target file which we don't want. e.g: date 2021-10-17, we don't want to remove the ```-``` – mma Oct 20 '21 at 08:25
  • No, ``'s/[^][A-Za-z0-9^`~!@#$%&*|,:;{}()+=_. "<>?\/\-]//g'`` won't remove hyphens. But I think now, you just want to keep alphanumeric and punctuation, right? Try `s/[^[:alnum:][:punct:]]//g` – Wiktor Stribiżew Oct 20 '21 at 08:34
  • doesn't work. ```'s/[^][A-Za-z0-9^`~!@#$%&*|,:;{}()+=_. "<>?\/\-]//g'``` will nullify the whole file. – mma Oct 21 '21 at 03:29
  • No idea what you mean by "nullify the file". Do you use `sed '...' file > file`? Pipe the result into the same file? – Wiktor Stribiżew Oct 21 '21 at 06:41
  • sed -e 's/[^][A-Za-z0-9^`~!@#$%&*|,:;{}()+=_. "<>?\/\-]//g' file >file2 file2 is empty – mma Oct 21 '21 at 08:13
  • What are the contents of `file`? – Wiktor Stribiżew Oct 21 '21 at 08:24

1 Answers1

0

Use this script:

> cat test.txt
$jeden$
#pięć#
!łzy!
> sed 's/[^][A-Za-z0-9^`~!@#$%&*|,:;{}()+=_. "<>?\/\-]//g' test.txt > test2.txt
> cat test2.txt
$jeden$
#pi#
!zy!

All characters different from those mentioned in negated brackets are deleted from test file and saved in test2 file safely.

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37