How to re-order pipe-delimited columns in Notepad++

notepad

I am trying to reposition every line in a .txt file in the following way below. However I have no idea on how to go about it. Is this possible with Notepad++?

From
apple|apple123@aol.com|orange
celery|celery@aol.com|cabbage
sandwich|sandwich@aol.com|turkey

To
apple|orange|apple123@aol.com
celery|cabbage|celery@aol.com
sandwich|turkey|sandwich@aol.com

Best Answer

Re-ordering Columns in a Text File

Yes this is possible within vanilla Notepad++, though as noted there are also plugins that will do it. A better (more robust) approach might be to use some command-line text-processing tools, but if you need a quick-and-dirty solution you can find that below:

Assuming your exact input (col1|col2|col3, pipe delimeter, no pipe in col2):

Find: (.*?)\|(.*?)\|(.*)

Replace: \1|\3|\2

works for me here in Notepad++, built Jan 2015. Somewhat brutish, but it works.

Explanation:

.* - matches any character (except newline), between zero and unlimited times

.*? - matches any character (except newline) as above, in a non-greedy manner (ie match as little as possible)

(.*) - plain brackets denote capturing group of above (to use in Replace as eg \1, \2, \3 etc )

\| - \ escapes pipe (|) to match it literally

\1|\3|\2 - print 1st matching group, pipe, third matching group, pipe, second matching group