Can anyone explain what is happening in "sed-regex here"

Viewed 73

I'm practising sed command using regex but the results are not as expected. I'm using terminal on mac Sierra. This is the input data:

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'm trying to swap first and second column. I used this command:

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

This command is returning the same input. However when I use,

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

the columns are getting swapped. why "+" is not matching since atleast one character is present in each row.

Also, when I use

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

The first parenthesis is capturing first two columns and second one last column,why the first parenthesis is not capturing first column?

2 Answers
Related