You can use the following sed command, let me know if you need additional explanations about it:
sed -E -i.bak 's/^([^\s]*)\s+([^,]*),[^,]*,\s*([^\s]*)\s+([^\s]*)\s*$/\4:\2:\1:\3/g' test_add_file.in;
TESTED on:
In a nutshell, you are define a regex that sed
will use to look in your text file to fetch the required patterns, then you use backreferences
to reuse the actual pattern that have been identified during the process.
For the exact syntax about the regex look at this link:
http://www.rexegg.com/regex-quickstart.html
Shortly:
^
beginning of line to fix the start of your pattern([^\s]*)
is used to get yourMickey, Minnie,...
(repetition of 0 to N non blank char that will are between parenthesis since this will be reused as back ref\s+
1 or more spaces betweenMickey and Mouse
([^,]*)
will match theMouse
part of the string (repetition of 0 to N non comma char),[^,]*,\s*
part between the 2 commas that is not used, 1112 Disney Dr.,
([^\s]*)
used to fetchOrlando, Anaheim
\s+
1 or more spaces betweenOrlando and FL
([^\s]*)
used to fetchFL, CA, ...
\s*$
fix the pattern end with$
atEOL
with eventually trailing spaces
then in the replacement part of your sed
command you reorder the patterns and you put column between them by \4:\2:\1:\3
1
solved Rearrange and delete fields with sed