你的代码对我来说没问题。
您可能还有其他东西,space
并且tabulation
...
hexdump -C
可能会帮助您检查问题所在:
awk '{gsub(/^[ \t]+|[ \t]+$/,""); print;}' in.txt | hexdump -C | less
更新:
好的,您确定了 DC4(可能还有其他一些控制字符......)
然后,您可以改进您的命令:
awk '{gsub(/^[[:cntrl:][:space:]]+|[[:cntrl:][:space:]]+$/,""); print;}' in.txt > out.txt
请参阅awk
手册页:
[:alnum:] Alphanumeric characters.
[:alpha:] Alphabetic characters.
[:blank:] Space or tab characters.
[:cntrl:] Control characters.
[:digit:] Numeric characters.
[:graph:] Characters that are both printable and visible. (A space is printable, but not visible, while an a is both.)
[:lower:] Lower-case alphabetic characters.
[:print:] Printable characters (characters that are not control characters.)
[:punct:] Punctuation characters (characters that are not letter, digits, control characters, or space characters).
[:space:] Space characters (such as space, tab, and formfeed, to name a few).
[:upper:] Upper-case alphabetic characters.
[:xdigit:] Characters that are hexadecimal digits.
前导/尾随0x20
删除
对我来说,命令没问题,我已经这样测试过:
$ echo -e "\x20 \tTEXT\x20 \t" | hexdump -C
00000000 20 20 09 54 45 58 54 20 20 09 0a | .TEXT ..|
0000000b
$ echo -e "\x20 \tTEXT\x20 \t" | awk '{gsub(/^[[:cntrl:][:space:]]+|[[:cntrl:][:space:]]+$/,""); print;}' | hexdump -C
00000000 54 45 58 54 0a |TEXT.|
00000005
但是,如果您0x20
在文本中间有
=>,则它不会被删除。
但这不是你的问题,不是吗?