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|[email protected]|orange
celery|[email protected]|cabbage
sandwich|[email protected]|turkey

To
apple|orange|[email protected]
celery|cabbage|[email protected]
sandwich|turkey|[email protected]

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
    Commented 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
    Commented Aug 3, 2015 at 5:25
  • @ExUmbris: usually the optimization starts when the speed seems inadequate
    – mike3996
    Commented Aug 3, 2015 at 9:52
  • OTOH deliberately writing a suboptimal query for the sake of not writing a ? character seems silly. Commented Aug 3, 2015 at 12:48
  • 1
    (.*?)|(.*?)|(.*) works for me.
    – Shaz
    Commented Aug 3, 2015 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .