使用sed提取字符串值

时间:2021-11-21 16:50:52

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

如何使用sed对字符串中由逗号分隔的子字符串组进行重新排序?

For instance,

例如,

hello bob, my name is, joseph

你好,鲍勃,我叫约瑟夫

becomes:

就变成:

joseph, My name is, hello bob

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

2 个解决方案

#1


2  

With this as the test file:

以此作为测试文件:

$ 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

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:

sed替代命令有s/old/new/。这将用新替换旧,而旧是正则表达式。在本例中,old是:

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

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:

parens中的项是组。这将行分隔为三个逗号分隔的组。我们可以将这三组分别称为\1、\2和\3。在新文本中,我们使用:

\3, \2, \1

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

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

Handling an indefinite number of columns

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是一个很好的工具:

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

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

-F', *'表示我们希望使用逗号作为字段分隔符。

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.

(我= NF;> 0;——)printf“% s % s”,我,美元(> 1 ?”,“\n”)在每个字段上反向循环,然后输出它,最后一个字段是换行符。

Reversing words within substrings

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

#2


0  

You can also use awk :

你也可以使用awk:

awk -F', ' '{ print $3 ", " $2 ", " $1 }' <<< "hello bob, my name is, joseph"
#joseph, my name is, hello bob

Update:
Based on @andlrc's comment, this solution is simpler:

更新:基于@andlrc的评论,这个解决方案更简单:

awk 'BEGIN{FS=OFS=", "}{print $3, $2, $1}' <<< "hello bob, my name is, joseph"

#1


2  

With this as the test file:

以此作为测试文件:

$ 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

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:

sed替代命令有s/old/new/。这将用新替换旧,而旧是正则表达式。在本例中,old是:

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

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:

parens中的项是组。这将行分隔为三个逗号分隔的组。我们可以将这三组分别称为\1、\2和\3。在新文本中,我们使用:

\3, \2, \1

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

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

Handling an indefinite number of columns

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是一个很好的工具:

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

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

-F', *'表示我们希望使用逗号作为字段分隔符。

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.

(我= NF;> 0;——)printf“% s % s”,我,美元(> 1 ?”,“\n”)在每个字段上反向循环,然后输出它,最后一个字段是换行符。

Reversing words within substrings

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

#2


0  

You can also use awk :

你也可以使用awk:

awk -F', ' '{ print $3 ", " $2 ", " $1 }' <<< "hello bob, my name is, joseph"
#joseph, my name is, hello bob

Update:
Based on @andlrc's comment, this solution is simpler:

更新:基于@andlrc的评论,这个解决方案更简单:

awk 'BEGIN{FS=OFS=", "}{print $3, $2, $1}' <<< "hello bob, my name is, joseph"