8

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

2

1 Answer 1

15

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

7
  • My pleasure, I'm glad it helped you and hope it will help others :-) It isn't necessary, but you can also click the tick mark to accept an answer if it solves your problem- you are free not to, or to wait for additional better answers too!
    – bertieb
    Aug 3, 2015 at 1:24
  • Don't you need to make the .* non-greedy, I.e. .*? to avoid a lot of backtracking?
    – Ex Umbris
    Aug 3, 2015 at 5:25
  • @ExUmbris: usually the optimization starts when the speed seems inadequate
    – mike3996
    Aug 3, 2015 at 9:52
  • OTOH deliberately writing a suboptimal query for the sake of not writing a ? character seems silly. Aug 3, 2015 at 12:48
  • 1
    (.*?)|(.*?)|(.*) works for me.
    – Shaz
    Aug 3, 2015 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.