使用 sed 删除标点符号和制表符
Removing punctuation and tabs with sed
我正在使用以下内容删除标点符号、制表符,并将文本文件中的大写文本转换为小写。
sed 's/[[:punct:]]//g' $HOME/file.txt | sed $'s/\t//g' | tr '[:upper:]' '[:lower:]'
我是否需要使用这两个单独的 sed
命令来删除标点符号和制表符,或者这可以通过单个 sed
命令完成吗?
此外,有人可以解释一下 $
在第二个 sed
命令中的作用吗?没有它,该命令不会删除选项卡。我查看了手册页,但没有看到任何提及此内容的内容。
输入文件如下所示:
Pochemu oni ne v shkole?
Kto tam?
Otkuda eto moloko?
Chei chai ona p’et?
Kogda vy chitaete?
Kogda ty chitaesh’?
单个 sed
具有多个 -e
表达式,可以按以下方式为 FreeBSD sed
完成
sed -e $'s/\t//g' -e "s/[[:punct:]]\+//g" -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' file
使用 y
限定词,
[2addr]y/string1/string2/
Replace all occurrences of characters in string1 in the pattern
space with the corresponding characters from string2.
如果在 GNU
sed 中,lower-case 转换的 \L
量词应该可以正常工作。
sed -e $'s/\t//g' -e "s/[[:punct:]]\+//g" -e "s/./\L&/g"
$''
是一个 bash
引用机制来启用 ANSI C-like escape sequences.
我正在使用以下内容删除标点符号、制表符,并将文本文件中的大写文本转换为小写。
sed 's/[[:punct:]]//g' $HOME/file.txt | sed $'s/\t//g' | tr '[:upper:]' '[:lower:]'
我是否需要使用这两个单独的 sed
命令来删除标点符号和制表符,或者这可以通过单个 sed
命令完成吗?
此外,有人可以解释一下 $
在第二个 sed
命令中的作用吗?没有它,该命令不会删除选项卡。我查看了手册页,但没有看到任何提及此内容的内容。
输入文件如下所示:
Pochemu oni ne v shkole?
Kto tam?
Otkuda eto moloko?
Chei chai ona p’et?
Kogda vy chitaete?
Kogda ty chitaesh’?
单个 sed
具有多个 -e
表达式,可以按以下方式为 FreeBSD sed
sed -e $'s/\t//g' -e "s/[[:punct:]]\+//g" -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' file
使用 y
限定词,
[2addr]y/string1/string2/
Replace all occurrences of characters in string1 in the pattern
space with the corresponding characters from string2.
如果在 GNU
sed 中,lower-case 转换的 \L
量词应该可以正常工作。
sed -e $'s/\t//g' -e "s/[[:punct:]]\+//g" -e "s/./\L&/g"
$''
是一个 bash
引用机制来启用 ANSI C-like escape sequences.