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?
source
share