<分区>
我需要在 bash shell 中以相反的顺序打印一个字符串。
[deeps@host1:~]echo maps.google.com|rev
moc.elgoog.spam
[deeps@host1:~]
但我需要它作为“com.google.maps”。对于以句点 (.
) 分隔的任何字符串,我通常都需要它。
它应该以相反的顺序打印。我该怎么做?
我也需要 Perl 中的解决方案。
<分区>
我需要在 bash shell 中以相反的顺序打印一个字符串。
[deeps@host1:~]echo maps.google.com|rev
moc.elgoog.spam
[deeps@host1:~]
但我需要它作为“com.google.maps”。对于以句点 (.
) 分隔的任何字符串,我通常都需要它。
它应该以相反的顺序打印。我该怎么做?
我也需要 Perl 中的解决方案。
最佳答案
按 拆分。
然后反转结果,然后再次将它们合并。
Perl Command Switches:
-l
means "remove/add the newline character from input/output."
-p
means "execute the program for each line of input, and print$_
after each execution."
-e
means "the following argument is the code of the program to run."
perldoc perlrun了解更多详情。
echo maps.google.com | perl -lpe '$_ = join ".", reverse split /\./;'
输出
com.google.maps
如果您有一个包含很多行的数据文件,它也适用。
输入
maps.google.com
translate.google.com
mail.google.com
运行
perl -lpe '$_ = join ".", reverse split /\./;' input
输出
com.google.maps
com.google.translate
com.google.mail
关于linux - 以相反顺序打印由点分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37474521/