On Tuesday, March 21, 2023 at 12:33:06 AM UTC+8, Janis Papanagnou wrote:
> On 20.03.2023 16:00,
hongy...@gmail.com wrote:
> > I want to replace all the non-printable/control characters with plain
> > space except keeping the `\n' as they are in the following string:
> I don't see any control characters in your data below.
If I first store the string here into a file named as `strfile' and then check it as follows, you will see them:
werner@X10DAi:~$ cat -A strfile
#g2 % point group to the space group of group$
3^I% generator$
0 -1 0$
0 0 -1$
-1 0 0$
3^I/8^I^I% generator$
-30 58 -30$
-33 55 -25$
-25 55 -33$
% order of the group unknown$
> >
> > ```
> > str:="""#g2 % point group to the space group of group
> > 3 % generator
> > 0 -1 0
> > 0 0 -1
> > -1 0 0
> > 3 /8 % generator
> > -30 58 -30
> > -33 55 -25
> > -25 55 -33
> > % order of the group unknown""";
> > ```
> >
> > Is there a convenient way to do this?
> Of course. Use variable substitution with patterns containing
> the respective character classes; for example
>
> str="..." # any string
> printf "%s" "${str//[^[:print:]$'\n']/ }"
>
> to replace all occurrences of non-printable and also not '\n'.
But your description above is inconsistent with the answer given by ChatGPT:
```
User:
str="..." # any string
printf "%s" "${str//[^[:print:]$'\n']/ }"
ChatGPT:
This code replaces any non-printable characters in the string "str" with a space. The "printf" command is used to print the modified string.
The "${str//[^[:print:]$'\n']/ }" part of the code is a parameter expansion that uses pattern substitution. The pattern "[^[:print:]$'\n']" matches any character that is not a printable character or a newline character. The substitution "/ " replaces each matched character with a space.
Overall, this code is useful for cleaning up strings and removing any non-printable characters that may cause issues in further processing or display.
User:
explain it in more detail
ChatGPT:
Sure, I'd be happy to explain this code in more detail!
The code you provided is a Bash script that uses the `printf` command to print a modified version of a string. The string is stored in the variable `str`, which is defined as a placeholder in the code you provided.
The `${str//pattern/replacement}` syntax is a parameter expansion that performs pattern substitution on the `str` variable. In this case, the pattern is `[^[:print:]$'\n']`, which matches any character that is not a printable character or a newline character. The `[:print:]` character class matches any printable character, which includes letters, numbers, punctuation, and whitespace. The `$'\n'` syntax is a Bash escape sequence that represents a newline character.
The replacement in this pattern substitution is a space character, denoted by the single space between the forward slashes. This means that any non-printable characters or newline characters in the `str` variable will be replaced with a space character.
The `printf` command then prints the modified string using the `%s` format specifier. This format specifier tells `printf` to print the argument as a string. The modified string is passed as an argument to `printf`, using the `${str//pattern/replacement}` syntax to substitute the modified string for the original `str` variable.
Overall, this code is a useful way to clean up strings and remove any non-printable characters or newline characters that may cause issues in further processing or display.
```
Finally, I only want to replace all occurrences of non-printable with one space and also keep '\n' as they are. The following is the desired result when applied on the file whose content is the string discussed here:
werner@X10DAi:~$ sed -e 's/[^[:print:]]/ /g' strfile | cat -A
#g2 % point group to the space group of group$
3 % generator$
0 -1 0$
0 0 -1$
-1 0 0$
3 /8 % generator$
-30 58 -30$
-33 55 -25$
-25 55 -33$
% order of the group unknown$
But my concern here is that is the whole file is represented in a string, I should do the above string operations on this string instead of a file.
> Janis
>
> >
> > Regards,
> > Zhao
> >