使用 GNU SED 制作巧妙的图案

使用 GNU SED 制作巧妙的图案

这是源字符串:

%5B++The+transmission+is+%5B150mhz%5D+The+year+is+%282017%29+This+is+%2A+great+%2A+so+far++%5D
  • 是否可以只用GNU SED到:
    1. 将单个 + 替换为单个空格
    2. %**abc"\x**"abc(% 后的前两个字符始终为十六进制 UTF-8)
    3. 每个句子开头和结尾都必须有一个“

所以结果应该是这样的:

"\x5B"  "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far"  "\x5D"

因此当 echo 与字符串一起使用时:

echo -e "\x5B"  "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far"  "\x5D"

结果将完全像这样:

[ The transmission is [150mhz] The year is (2017) This is * great * so far ]

答案1

这有效:

sed -r -e 's/(.*)/"\1"/' -e 's/\+/" "/g' -e 's/""/ /g' -e 's/\%/\\x/g' -e 's/("\\x.{2})/\1"/g' -e 's/""\s+/" /g' -e 's/"(.*)"/"\1/' -e 's/([^"]|(([0-9]|[a-z])))(\\x[0-9]([a-zA-Z]|[0-9]))" /\1"\4" /g' src.txt

结果:

"\x5B"  "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far"  "\x5D"

然后:

echo -e "\x5B"  "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far"  "\x5D"

结果:

[ The transmission is [150mhz] The year is (2017) This is * great * so far ]

我不认为这sed是这里最好的工具,但既然你想学习。

相关内容