sed: Remove value and insert at the end

I have following Strings

"1 Michael"
"2 John" ...

and I´d like to transform them into

"Michael 1"
"John 2" ....

How can I do this?

The Strings are stored in one shell variable and each string represents a seperate line.

So when calling echo "$var" it prints

1 Michael
2 John
Asked By: Justin

echo "$string" | sed -E 's/([[:digit:]]+) (.*)/2 1/'
Answered By: glenn jackman

Swapping the two whitespace-delimited fields with awk:

awk '{ print $2, $1 }' file >outfile

The output is written as a space-delimited file to the file called outfile.


$ cat file
1 Michael
2 John
$ awk '{ print $2, $1 }' file >outfile
$ cat outfile
Michael 1
John 2
Answered By: Kusalananda
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.