Can someone explain what is happening in "sed-regex here",

I am using the sed command with regex, but the results are not as expected. I am using a terminal on a Mac Sierra. This is the input:

Mark watermellons 12
Robert pears 4
Terry oranges 9
Lisa peaches 7
Susy oranges 12
Mark grapes 39
Anne mangoes 7
Greg pineapples 3
Oliver rockmellons 2
Betty limes 14

I am trying to swap the first and second columns. I used this command:

sed 's/\(.+\) \(.+\) /\2 \1/ ' file.txt

This command returns the same input. However, when I use

sed 's/\(.*\) \(.*\) /\2 \1 /' file.txt

the columns are swapped. why the “+” does not match, because at least one character is present on each line.

Also, when I use

sed 's/\(.*\) \(.*\)/\2 \1 /' file.txt 

The first bracket includes the first two columns and the second last column, why doesn't the first bracket capture the first column?

+4
source share
2 answers

-. , + .

sed + " ", . BSD sed ( OSX) -E, :

sed -E 's/(.+) (.+) /\2 \1/ ' file.txt

, + - , :

sed 's/\(..*\) \(..*\) /\2 \1/' file.txt

Btw, BSD sed GNU sed. , GNU sed, BSD sed:

sed 's/\(.\+\) \(.\+\) /\2 \1/ ' file.txt

GNU, BSD sed. , , , .

+8

Casimir et Hippolyte , + sed.

s/\(.*\) \(.*\) /\2 \1 / : " 0 , , 0 , . 2 , 2 .

s/\(.*\) \(.*\)/\2 \1 / " 0 , , 0 . 1 .* , , , , .* 0 .

+3

Source: https://habr.com/ru/post/1681549/


All Articles