[Solved] Rearrange and delete fields with sed


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:

enter image description here

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 your Mickey, 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 between Mickey and Mouse
  • ([^,]*) will match the Mouse 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 fetch Orlando, Anaheim
  • \s+ 1 or more spaces betweenOrlando and FL
  • ([^\s]*) used to fetch FL, CA, ...
  • \s*$ fix the pattern end with $ at EOL 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