Transpose function in Notepad++
Asked Answered
C

4

6

I have a text file as:

0xC1,0x80,
0x63,0x00,
0x3F,0x80,
0x01,0xA0,

I want output as:

Line1: 0xC1,0x63,0x3F,0x01,
Line2: 0x80,0x00,0x80,0xA0,

How to do this using replace function in Notepad++?

Chopfallen answered 18/11, 2013 at 10:21 Comment(1)
I'm not sure it is possible with N++. You'd better use a script to do such tranformation.Weary
M
7

You can use the below shortcuts to do the transpose in Notepad ++

 Step 1: Ctrl + A: selects all.
 Step 2: Ctrl + J: Transpose the Row you selected
Modest answered 23/1, 2019 at 18:11 Comment(2)
Thanks for sharing this tip as earlier tip to use the replace with regex \r\n did not work. Ctrl+J work for my npp v7.8.6 (64 bit on win10 enterprise) and in another npp v7.6.4 (win server 2019 deployed by IT to use in citrix)Thermosiphon
Note that Ctrl + J is the Edit > Line Operations > Join Lines option. The Find and Replace answer from @Noel is more general and can be preferred in most cases.Yardage
C
5
  1. Use the box select feature to select the second column text.
    • Use Alt+Shift+Arraw keys to select the second column.
  2. Copy the selected text to a new file.
  3. Use Find/Replace to remove all the newline characters.
    • Ctrl+F to open find/replace dialog box.
    • Select either Extended or Regular Expression Serach mode.
    • Type \r\n in Find What box.
    • Keep the Replace with box blank.
    • Click on Replace All in ALL Open Documents.
    • Now, the text is brought in single line.
  4. Copy the text from second file and paste it to second line of first file.

Cheers...

Critique answered 19/11, 2013 at 16:35 Comment(0)
B
4

There is no built-in function in Notepad++ for transposing a matrix and you can't do it using Replace (as M42 pointed out). Also, I'm not aware of any related plugin. So you will either need a different editor or do it with a script. The simplest solution I guess using a Spreadsheet, eg Excel or OpenOffice, both of them allow you to easily transpose a table.

But, there's still a good alternative without leaving Notepad++. Is to use the Python Script plugin.

Setup Python Script plugin

  1. Install Python Script plugin, from Plugin Manager or from the official website.
  2. When installed, go to Plugins > Python Script > New Script. Choose a filename for your new script (eg transpose.py) and copy the first code block that follows and copy the second one to another script, called for example transpose_uneven.py.
  3. Open your data file and then run Plugins > Python Script > Scripts > transpose.py. This will open a new tab with your data transposed.

transpose.py

delimiter=","
newline="\n"
content=editor.getText()
matrix=[line.split(delimiter) for line in content.rstrip(newline).split(newline)]
transposed=list(map(list, zip(*matrix)))
notepad.new()
for line in transposed:
    editor.addText(delimiter.join(line) + newline)
if len(transposed)!=len(matrix[0]):
    console.clear()
    console.show()
    console.write("Warning: some rows are of uneven length. You might consider using the transpose_uneven script instead.")

transpose_uneven.py

import itertools    
delimiter=","
newline="\n"    
content=editor.getText()
matrix=[line.split(delimiter) for line in content.rstrip(newline).split(newline)]
transposed=list(map(list, itertools.izip_longest(*matrix, fillvalue="")))
notepad.new()
for line in transposed:
    editor.addText(delimiter.join(line) + newline)

Examples

The transpose.py script will transpose the following example:

0xC1,0x80,
0x63,0x00,
0x3F,0x80,
0x01,0xA0,

To:

0xC1,0x63,0x3F,0x01
0x80,0x00,0x80,0xA0
,,,

If some of your rows are uneven:

0xC1,0x80,
0x63,0x00,
0x3F,0x80,
0x01,0xA0,
0x02

The uneven columns will be discarded accordingly:

0xC1,0x63,0x3F,0x01,0x02

If this is not desired, use transposed_uneven.py and it will return:

0xC1,0x63,0x3F,0x01,0x02
0x80,0x00,0x80,0xA0,
,,,,
Burney answered 18/11, 2013 at 13:21 Comment(0)
P
0

If you really have such a fixed format and need such a fixed output i normally try it with an instant macro.

So my cursor is in the top left corner of the file ready to manipulate and i press the record button (or within the menu bar Macro - Start recording).

In you specific case now press:

  • End
  • Del
  • Pos1

End hit the stop button (or within the menu bar Macro - Stop recording).

Now for a first test hit the playback button (or within the menu bar Macro - Playback) and test if it works. If yes click on Macro - Run a macro multiple times and select Run until the end of file.

Paulina answered 18/11, 2013 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.