dlh*_*lhi 4 text-processing locale
我有一个包含 10,000 行的文件,每行末尾都有一个数字,例如:
\nasdf ggg \xd9\xa1\ngghh rtt \xd9\xa3\xd9\xa2\xd9\xa5\ntyyu bnnn jigff \xd9\xa2\xd9\xa7\n
Run Code Online (Sandbox Code Playgroud)\n将所有数字的数字按相反顺序排列。正确的顺序是:
\nasdf ggg \xd9\xa1\ngghh rtt \xd9\xa5\xd9\xa2\xd9\xa3\ntyyu bnnn jigff \xd9\xa7\xd9\xa2\n
Run Code Online (Sandbox Code Playgroud)\n你能帮我(请)颠倒每行中数字的顺序吗?
\n\n(\xd9\xa0\xd8\x8c \xd9\xa1\xd8\x8c \xd9\xa2\xd8\x8c \xd9\xa3\xd8\x8c \xd9\xa4\xd8\x8c \xd9\xa5\xd8\x8c \ xd9\xa6\xd8\x8c \xd9\xa7\xd8\x8c \xd9\xa8\xd8\x8c \xd9\xa9)
\n使用 Perl:
\n$ perl -CSD -pe \'s/(\\d+)$/reverse($1)/e\' file\nasdf ggg \xd9\xa1\ngghh rtt \xd9\xa5\xd9\xa2\xd9\xa3\ntyyu bnnn jigff \xd9\xa7\xd9\xa2\n
Run Code Online (Sandbox Code Playgroud)\n这会将替换命令应用于每行输入。替换匹配行末尾的任何数字字符串。无论匹配什么数字串,该reverse()
函数都会将其反转。还原的结果用作替换中的替换文本。
尾随/e
导致 Perl 将 视为reverse($1)
要计算的命令。
-CSD
启用 UTF-8 I/O的选项perl
。