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
echo "$string" | sed -E 's/([[:digit:]]+) (.*)/2 1/'
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
.
Testing:
$ cat file
1 Michael
2 John
$ awk '{ print $2, $1 }' file >outfile
$ cat outfile
Michael 1
John 2