本文介绍了使用 sed 提取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们将如何使用 sed 重新排序以逗号分隔的字符串中的子字符串组?

How would someone use sed to go about reordering groups of substrings within a string separated by commas?

例如,

你好鲍勃,我叫约瑟夫

变成:

约瑟夫,我的名字是,你好鲍勃

推荐答案

以此为测试文件:

$ cat file
hello bob, my name is, joseph

我们可以根据需要对字段重新排序:

We can reorder the fields as you like with:

$ sed -E 's/([^,]*), *([^,]*), *([^,]*)/\3, \2, \1/' file
joseph, my name is, hello bob

工作原理

sed 替代命令的形式为 s/old/new/.这将 old 替换为 new,其中 old 是一个正则表达式.在这种情况下,old 是:

How it works

A sed substitute command has the form s/old/new/. This replaces old with new where old is a regex. In this case, old is:

([^,]*), *([^,]*), *([^,]*)

括号中的项目是组.这会将行分成三个逗号分隔的组.我们可以将这三组分别称为\1\2\3.在 new 文本中,我们使用:

The items in parens are groups. This separates the line into three comma-separated groups. We can refer to these three groups as \1, \2, and \3 respectively. In the new text, then, we use:

\3, \2, \1

这会颠倒组的顺序,按照您的要求将第三个放在最前面,将第一个放在最后.

This reverses the order of the groups, putting the third first and the first last, as you requested.

如果我们想反转所有的子串但事先不知道子串的个数,那么awk是一个很好用的工具:

If we want to reverse all the substrings but the number of substrings is unknown in advance, then awk is a good tool to use:

$ awk -F', *' '{for (i=NF;i>0;i--)printf "%s%s",$i,(i>1?", ":"\n")}' file
joseph, my name is, hello bob

-F', *' 表示我们要使用逗号(可选)后跟空格作为字段分隔符.

-F', *' indicates that we want to use a comma optionally followed spaces as the field delimiter.

for (i=NF;i>0;i--)printf "%s%s",$i,(i>1?", ":"\n") 循环在每个字段上反转并打印它,然后是 或最后一个换行符.

for (i=NF;i>0;i--)printf "%s%s",$i,(i>1?", ":"\n") loops in reverse over each field and prints it followed either by , or, for the last one, a newline.

这是一个在子字符串中反转单词的示例:

Here is an example of reversing words within a substring:

$ sed -E 's/([^ ,]*) ([^,]*), /\2 \1, /' file
bob hello, my name is, joseph

这是一个反转子串中单词同时反转子串顺序的示例:

Here is an example of reversing the words within a substring while also reversing substring order:

$ sed -E 's/([^ ,]*) ([^,]*), *([^,]*), *([^,]*)/\4, \3, \2 \1/' file
joseph, my name is, bob hello

这篇关于使用 sed 提取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:09