如何将文件中除换行符以外的每个字符都加倍?

Pau*_*ran 3 text bash files

? 我可以将文件中除换行符以外的每个字符都加倍吗??t 应该是这样的:

之前的文件内容:

echo hello world
Run Code Online (Sandbox Code Playgroud)

之后的文件内容:

eecchhoo  hheelllloo  wwoorrlldd
Run Code Online (Sandbox Code Playgroud)

ste*_*ver 5

使用 sed:

sed 's/./&&/g' yourfile
Run Code Online (Sandbox Code Playgroud)

前任。

$ echo 'echo hello world' | sed 's/./&&/g'
eecchhoo  hheelllloo  wwoorrlldd
Run Code Online (Sandbox Code Playgroud)

或者,使用 Perl 的字符串乘法运算符:

$ echo 'echo hello world' | perl -lne 'print map { $_ x 2 } split //'
eecchhoo  hheelllloo  wwoorrlldd
Run Code Online (Sandbox Code Playgroud)

当然可以在 awk 中进行字符串连接,但 AFAIK 并非没有对字符进行显式循环:

$ echo 'echo hello world' | awk 'BEGIN{OFS=FS=""} {for(i=1;i<=NF;i++) $i = $i $i}1'
eecchhoo  hheelllloo  wwoorrlldd
Run Code Online (Sandbox Code Playgroud)