linux - 以相反顺序打印由点分隔的字符串

标签 linux string bash perl shell

<分区>

我需要在 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/

相关文章:

c - 将字符串的第一部分转换为 C 中的数字

linux - 如何并行化我的 bash 脚本以与 `find` 一起使用而不面临竞争条件?

linux - 使用 ctrl 评论 -/jupyter notebook 在 Linux Mozilla Fire Fox 上不起作用

java - 在 Ubuntu Linux 中将 Java 1.6 默认设置为 1.7

linux - nginx 的 apt-get 安装留下 root 拥有的文件

c++ - 在从不再加载的动态库实例化的对象上使用在主代码库中定义的模板类方法时出现段错误

regex - 使用python正则表达式转义无效的 Markdown

C++, boost : which is fastest way to parse string like tcp://adr:port/into address string and one int for port?

bash - 在特定数量的分隔符后添加 '\n'

objective-c - 我如何制作 GIT 凭证助手?