Transpose function in Notepad++ - replace

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++?

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

Use the box select feature to select the second column text.
Use Alt+Shift+Arraw keys to select the second column.
Copy the selected text to a new file.
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.
Copy the text from second file and paste it to second line of first file.
Cheers...

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
Install Python Script plugin, from Plugin Manager or from the official website.
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.
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,
,,,,

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.

Related

Regex/Notepad++ to extract text from file

I have multiple files with text in parenthesis that I need to extract from the file (or delete everything else in that file). I have a method that works, but it only works for one file. Here is an example of the kind of files I'm dealing with.
(is it on?)
[3.87595 3.87595 0 ]xsh
grestore
NDTMRY+Helvetica[8.5 0 0 -8.5 0 0 ]msf
mo
(NO)
The method I have used is as follows:
in notepad++ under the mark tab in find replace; Find: ^(.*?$ (with bookmark line checked)
Search>bookmarks>remove unbookmarked lines
Is there a way/better way to do this for multiple files at a time? In this or another language such as python.
Thanks!
Yes, it is possible to remove in multiple files lines that do not start with (.
Here is the screenshot with settings:
So, here are the instructions:
Press Ctrl+H and click Find in Files
In Find What, type ^(?!\().*\R*, keep Replace With empty
Add file masks in Filters
Select the initial directory in Directory.
Make sure Regular expression radio button is selected.
Adjust other options and hit Replace in Files button.

Find and replace in Visual Studio code in a selection

I have the following line in a file I'm editing in VSCode:
...............111.........111.............111..
I want to replace all .s with 0s. However, when I highlight the line and do a find/replace for .s, all the .s in the document are replaced, not just the ones in the line I've select, even when I toggle the "Find in selection" button. Is this a bug? In other editors, if I select a chunk of text and then do a find/replace, it will only find/replace matches within the selected block.
Below is a snippet that you should be able to reproduce the issue with. The ...............111.........111.............111.. line is inside the test_unicode function.
def test_simple2(self):
"""Simple CSV transduction test with empty fields, more complex idx, different pack_size.
100011000001000 ->
..........111....................111..........11111..........111..
"""
field_width_stream = pablo.BitStream(int('1000110001000001000', 2))
idx_marker_stream = pablo.BitStream(int('11101', 2))
pack_size = 4
target_format = TransductionTarget.JSON
csv_column_names = ["col1", "col2", "col3", "col4", "col5"]
pdep_marker_stream = pablo.BitStream(generate_pdep_stream(field_width_stream,
idx_marker_stream,
pack_size, target_format,
csv_column_names))
self.assertEqual(pdep_marker_stream.value, 63050402300395548)
def test_unicode(self):
"""Non-ascii column names.
Using UTF8. Hard coded SON boilerplate byte size should remain the same, column name
boilerplate bytes should expand.
100010010000000 ->
2 + 4 + 9 2 + 4 + 6 2 + 4 + 7
...............111.........111.............111..
"""
field_width_stream = pablo.BitStream(int('100010001000', 2))
idx_marker_stream = pablo.BitStream(1)
pack_size = 64
target_format = TransductionTarget.JSON
csv_column_names = ["한국어", "中文", "English"]
pdep_marker_stream = pablo.BitStream(generate_pdep_stream(field_width_stream,
idx_marker_stream,
pack_size, target_format,
csv_column_names))
self.assertEqual(pdep_marker_stream.value, 1879277596)
I'm using VSCode 1.12.2 in Ubuntu 16.04.
I was able to get it to work but the workflow is poor:
control + H to open Find/Replace
Select your line of text
Click the "Find in selection" icon to the right Alt L or ⎇ ⌘ L on macOS)
Enter your find and replace characters in their inputs
Click the Replace all icon
It works but you have to go through the workflow all over again for each new selection (except for CTR + H of course). BTW I have the exact same behavior in Sublime Text.
Could you go with a regExp to find your lines? Do they contain only .'s and 1's?
This is a more general answer for other users who come here just wanting to use basic find and replace functionality.
On Mac you can press Command + Option + F to open Find and Replace:
Alternatively, you can press Command + F to open Find and then click the little triangle on the left to show the Replace field:
From the VSCode devs:
We used to enable find in selection automatically when opening the
find widget with a selection, but it was too easy to trigger
accidentally and produced a lot of complaints. You probably want to
set "editor.find.autoFindInSelection": true which will make it work
the way you expect.
The VSCode GitHub issue has more details if anyone is interested.
EDIT: The autoFindInSelection option is available starting from VSCode 1.13. That version is currently in development (as of 6/7/2017), so this fix won't work until the new version is released.
I found the following workflow to be fairly painless:
Select text region with mouse or keyboard.
Ctrl+H to toggle find and replace
Alt+L to toggle find in selection
Ctrl+Alt+Enter to replace all (or enter to replace individually)
Since sometimes we might have similarly named things so you don't want to select everything, one of my favorites shortcut sequences is to select the next occurrence:
Use shift and arrows to highlight the term you want to match.
Use Ctrl + d to highlight the next occurrence of the term.
The Basic Editing in VS Code documentation page has some extremely useful variations on find and replace. One extremely useful shortcut is the Column (Box) Selection.
Just ran into this, my solution was to do
command + N to create a new file
paste my selection in there
do my find and replace operations on that while file
copy result back on top of my original selection
On Mac:
Select the text
Press command + shift + L
For mac
Press command + option + f to bring up this menu:
Press the little icon that has the arrow pointing at it above (3 horizontal bars)
Select the text you want to do a find and replace in, and enter the 'find' and 'replace' fields
Press this icon:
That's all!
In 2022, there's a bug to be wary of 🐛
There is a silent bug (I'll add more about this as I learn more about it). But sometimes find and replace within selection doesn't find the values, even if you can see them with your own eyes. This is dangerous because you could think you've replaced them all but it really hasn't.
So do these two things:
a visual check after doing a find and replace (to make sure it worked)
if vscode completely ignores you (and doesn't do the find and replace within selection after you've followed the above instructions), close the find and replace box by pressing the "x" in the corner, and retry the sequence of steps (it worked for me after closing and retrying).
Okay, this is really dumb, at first I felt really stupid when I finally found this, then I realized its just VS Code which has a bad interface.
The key is, there are TWO TOOLS here,
Search/Replace (the pane on the left at the top of the Explorer) and Find/Replace (which is a dialogue which opens when you press CTRL-F)
THESE ARE NOT THE SAME TOOL!!
SEARCH-REPLACE is a tool written for project-wide searches and
changes
FIND-REPLACE is a small dialogue best suited for more
surgical editing.
i.e. you should use FIND-REPLACE!
find replace window image with find-in-selection highlighted
Also, its SUPER IMPORTANT to follow these steps in the right order, or it doesn't give the expected results.
Press CTRL-F to open the find dialogue (usually opens in the top right)
press the little arrow to the right of the find field which opens the replace input field
ensure "find in selection" is turned off (i.e. not highlighted)
type in the fields the strings you want to search/replace
select the text you want to do a search/replace within.
Now press "find in selection" (or type ALT-L)
You should see only highlighting in the area you previously selected.
Now you can click either "replace all" (CTRL-ALT-ENTER) or line-by-line "replace" (ENTER)
I hope this helps.
My suggestion to VSC developers, there should be a refresh button so that after you have selected the area of interest, and you already have your find and replace strings defined, you can select a new region and click "refresh find" instead of needing to repeat steps 3 to 8.
For those where it still does not work, there is one step omitted in all of the above answers: Uncheck "Find in selection" if it is checked (which it probably is when you are struggling with it and in despair googled the problem, and then found this SO entry). Only then select the lines and then re-check "Find in selection".
For Ubuntu, highlight the lines where you want to make changes, press alt-L and then ctrl-H. Then type the name you want to replace and replacement name in the top right dropdown.
Or some combination of these actions depending on whether the dropdown is already open or you're already in alt-L mode. I'm still learning but thought I'd share what's working.

How do I find and replace all occurrences (in all files) in Visual Studio Code?

I can't figure out how to find and replace all occurrences of a word in different files using Visual Studio Code version 1.0.
I get the impression this should be possible since doing Ctrl + Shift + F allows me to simply search a folder, but i am clueless to how to proceed from here.
I looked in various key combination
https://code.visualstudio.com/Docs/customization/keybindings
But I only found how to replace all occurrences in the open file (Ctrl + h)
All help gratefully appreciated, this is driving me crazy.
I'm using Visual Studio Code 1.8, and this feature is available. But it's a little tricky to understand at first, and (at the time of writing) the docs don't explain clearly how to use it, so here's how it works, step by step:
Invoke Replace in Files (under the Edit menu, or with shortcut Ctrl+Shift+H)
You'll see a standard Find/Replace input replacing the files pane on the left:
Enter your search string and the replace string, then press enter. It may churn for a second searching all files, then it'll show the proposed changes in all your project files -- but note, these changes haven't been made yet! Here's what it looks like:
Now you need to make the changes (and even after that, you have to save the modified files.)
You can make those changes in various ways:
Make all changes to all files at once.
Click the replace icon next to your replace string (note: you'll get a dialog to confirm this bulk action.)
Make all changes in a single file at once.
Click the replace icon next to the filename (note: the icon only shows up when you hover over the filename row)
Make a single change in a single file.
Click the replace icon next to the individual change: (note: the icon only shows up when you hover over the change row)
Finally, don't forget to save!
All those files are now modified in the editor and not yet saved to disk (unless Auto Save is on).
Use File -> Save All (or Ctrl+Alt+S)
Update: I'm not sure when this was added, but if you click the "Replace all" button and see this dialog, clicking "Replace" will change and save all files in one click:
Since version 1.3 of vscode this is possible
Navigate to the search, click icon to the left or:
(mac) cmd + shift + h
(PC) ctrl + shift + h
expand replace
enter search term and replace term
confirm!
To replace a string in a single file (currently opened): CTRL + H
For replacing at workspace level use: CTRL + SHIFT + H
Update for 2020
If you are using the search feature to search across files (Ctrl + Shift + F) it can be easy to miss how to convert your search to a search and replace within the UI.
Here's a typical search result:
To convert this to a search and replace you need to click the arrow icon to the left of the search input field. This will open the replace options as seen below. Note the arrow icon is now pointed down.
The keyboard shortcut Ctrl + Shift + H will also work as well to access the search and replace.
Link to VSCode docs on search and replace: https://code.visualstudio.com/docs/editor/codebasics#_search-and-replace
This is the best way.
First put your cursor on the member and click F2.
Then type the new name and hit the Enter key. This will rename all of the occurrences in every file in your project.
This is ideal for when you want to rename across multiple files. For example, you may want to rename a publicly accessible function on an Angular service and have everywhere that uses it get updated.
For more great tools I highly recommend:
https://johnpapa.net/refactoring-with-visual-studio-code/
On the Visual Studio Code Key Bindings page, the section Keyboard Shortcuts Reference has links to a PDF for each major OS. Once open, search for "replace in files" or any other shortcut you might need.
Another way is to use the Command Palette (ctrl/cmd+shift+P) where you can type "replace" to list all related commands, including the one you want:
Replace in Files ctrl/cmd+shift+H
Visual Studio Code: Version: 1.53.2
If you are looking for the answer in 2021 (like I was), the answer is here on the Microsoft website but honestly hard to follow.
Go to Edit > Replace in Files
From there it is similar to the search funtionality for a single file.
I changed the name of a class I was using across files and this worked perfectly.
Note: If you cannot find the Replace in Files option, first click on the Search icon (magnifying glass) and then it will appear.
There are 2 methods technically same
First put your cursor on the word and press F2.
Replace your word and press Enter.
First put your cursor on the word and left click it.
Click "Rename Symbol" option.
Replace your word and press Enter.
In the VSC version 1.57
you can use the F2 key to replace on the folder(Rename Symbol)
Only thing is that even though all the places inside folder are replaced the file is not saved and the save has to be done afterwards
Also, instead of presing enter and replace all directly, you can press Shift + Enter to preview the places that are going to be renamed.
Step by step with images
Nowadays in VS Code this can be done quickly.
1) Click on the Search Button (in the side bar):
2) Enter in the inputs what do you want to find, and with what do you want to replace it:
(Here I am replacing in my project all the occurrences of table-cell with t-cell)
3) Finally, click on this small button (Replace All):
Then a pop-up will appear to confirm if you want to replace all occurrences. Click on Replace.
And you're done!
For search and replace all use this command:
command + option + F

Notepad++ search with "Bookmark line" option doesn't bookmark all the lines that match a multi-line regex, but only the first one

I want to filter everything from a log that belongs to a particular user.
With the following pattern, and the ". matches newline" option enabled, I can match everything that I am looking for, but when I ask Notepad++ to bookmark these line so I can copy them, something strange happens.
([^\n]+)userB(.+?)(?=([0-9]{4}-[0-9]{2}-[0-9]{2}))
In front of line 2 and 8 I see a bookmark icon, but the lines: 3,4,5 and 9,10,11 are missing an icon, although they belong to the highlighted text.
Why does Notepad++ highlight the text, but doesn't place the bookmark correctly? And more importantly, how can I fix this?
Here is the log that I am using:
2015-03-02 11:28:44,993 INFO application [http-0.0.0.0-8080-17] userA 99:7 12345 some message
2015-03-02 11:28:45,468 WARN application [http-0.0.0.0-8080-9] userB 12:2 some message
extra information
at some.classes.and.function(Filename.java:123)
at some.classes.and.function(Filename.java:123)
2015-03-02 11:28:44,993 INFO application [http-0.0.0.0-8080-17] userA 99:7 12345 some message
2015-03-02 11:28:44,993 INFO application [http-0.0.0.0-8080-17] userA 99:7 12345 some message
2015-03-02 11:28:45,468 WARN application [http-0.0.0.0-8080-9] userB 12:2 some message
extra information
at some.classes.and.function(Filename.java:123)
at some.classes.and.function(Filename.java:123)
2015-03-02 11:28:44,993 INFO application [http-0.0.0.0-8080-17] userA 99:7 12345 some message
The following will extract the information you want without using bookmarks and should work in Notepad++ version 6.9.1 onwards.
It assumes that the lines of extra information don't start with a digit.
It deletes the copied lines from the data file so work on a copy of the data file if you want to preserve it.
It is not very efficient because the switch tab command positions the cursor back at the start of a tab's text.
It duplicates the last match for userB. (A minor irritant!)
Description
1) Define shortcuts for switching to next & previous tabs
2) Open the data file with Notepad++
3) Open a new tab then go back to the data file tab
4) Define a macro `Cut User data. Paste in next tab`
5) Run the macro `Cut User data. Paste in next tab` on the data file to `end of file`
(It needs to be run once prior to running it in `Run a Macro Multiple Times`
because of a quirk of Notepad++)
Method
1) Settings Shortcut Mapper Main menu
Switch to previous document = Ctrl+Alt+Left
Switch to next document = Ctrl+Alt+Right
Close
2) Open data file with Notepad++
3) File New Ctrl+Alt+Left
4) Start Recording
Find Find what: .+ userB.+\r\n(\D.+\r\n)*
Search Mode = Regular expression
Find Next Alt+F4
Ctrl+X Ctrl+Alt+Right
Ctrl+End Ctrl+V Ctrl+Alt+Left
Stop recording
Save Current Recorded Macro
Name: Cut User data. Paste in next tab OK
5) Macro Cut User data. Paste in next tab
Run a Macro Multiple Times
Macro to run: Cut User data. Paste in next tab
Run until end of file = Yes
Run then when it has stopped Cancel
(Editted to be an answer.)
At least in the current version of Notepad++ the macro halts while the switch dialog box appears when using the macro solution and switching tabs.
You must first go to settings, MISC., document switcher, and disable both check boxes. This is at least true in v7.6.

Can Notepad++ save out search results to a text file?

I need to do quite a few regular expression search/replaces throughout hundreds and hundreds of static files. I'm looking to build an audit trail so I at least know what files were touched by what searches/replaces.
I can do my regular expression searches in Notepad++ and it gives me file names/paths and number of hits in each file. It also gives me the line #s which I don't really care that much about.
What I really want is a separate text file of the file names/paths. The # of hits in each file would be a nice addition, but really it's just a list of file names/paths that I'm after.
In Notepad++'s search results pane, I can do a right click and copy, but that includes all the line #s and code which is just too much noise, especially when you're getting hundreds of matches.
Anyone know how I can get these results to just the file name/paths? I'm after something like:
/about/foo.html
/about/bar.html
/faq/2012/awesome.html
/faq/2013/awesomer.html
/foo/bar/baz/wee.html
etc.
Then I can name that file regex_whatever_search.txt and at the top of it include the regex used for the search and replace. Below that, I've got my list of files it touched.
UPDATE What looks like the easiest thing to do (at least that I've found) is to just copy all the search results into a new text file and run the following regex:
^\tLine.+$
And replace that with an empty string. That'll give you just the file path and hit counts with a lot of empty space between each entry. Then run the following regex:
\s+\n
And replace with:
\n
That'll strip out all the unwanted empty space and you'll be left with a nice list.
maybe you need power of unix tools
assume you have GNUWin32 installed in c:\tools\gnuwin32
than if you have replace.bat file with that content:
#echo off
set BIN=c:\tools\gnuwin32\bin
set WHAT=%1
set TOWHAT=%2
set MASK=%3
rem Removing quotes
SET WHAT=###%WHAT%###
SET WHAT=%WHAT:"###=%
SET WHAT=%WHAT:###"=%
SET WHAT=%WHAT:###=%
SET TOWHAT=###%TOWHAT%###
SET TOWHAT=%TOWHAT:"###=%
SET TOWHAT=%TOWHAT:###"=%
SET TOWHAT=%TOWHAT:###=%
SET MASK=###%MASK%###
SET MASK=%MASK:"###=%
SET MASK=%MASK:###"=%
SET MASK=%MASK:###=%
echo %WHAT% replaces to %TOWHAT%
rem printing matching files
%BIN%\grep -r -c "%WHAT%" %MASK%
rem actual replace
%BIN%\find %MASK% -type f -exec %BIN%\sed -i "s/%WHAT%/%TOWHAT%/g" {} +
you can do regex replace in masked files recursively with output you required
replace "using System.Windows" "using Nothing" *.cs
The regulat expression I use for this kind of problem is
^\tLine.[0-9]*:.
And it works for me
This works well if you have Excel available and want to avoid using regular expressions:
Ctrl+A to select all the results
drag & drop the selected results to Excel
Create a Filter on the 1st row
Filter out the lines that have "(Blank)" on the 1st column
Select the remaining lines (i.e. the lines with the filenames) and copy/paste them to another sheet or any wanted destination
You could also Ctrl+A, Ctrl+C the search results, then use the Paste Option "Use Text Import Wizard" in Excel, say that the data is "Fixed width" and put one single break line after the 2nd character (to remove the two leading spaces in the filename during import), and use a filter to filter out the unwanted rows.