Various Ways of Working With Lots of Text and Files

Table of contents

  • Find and replace text in a single text file
  • Find and replace text in multiple files
  • Find and replace multiple regexes in all files in a folder recursively
  • Sort a list of text
  • Get unique / remove duplicate lines in a list of text
  • Select a column of text
  • Find text in a single file and copy all matches to the clipboard
  • Add text to the beginning of each line of text
  • Copy differences (diff) between two files
  • Rename multiple filenames in multiple folders

The instructions below use Visual Studio Code unless stated otherwise.

Find and replace text in a single text file

  1. Open the text file in Visual Studio Code
  2. Click Edit > Find or Edit > Replace
  3. Enter your search term and replace term
  4. Use optional options like
    • case senstive match
    • match whole word
    • regular expression
    • preserve case
    • replace first match
    • replace all matches at once
    • find in selection

Find and replace text in multiple files

  1. In Visual Studio Code, right click on a folder and click “Find in Folder…”
  1. Enter your search term and replace term
  2. Use optional options like
    1. case senstive match
    2. match whole word
    3. regular expression
    4. preserve case
  3. In the search results, you will see a preview of the text change.

If you click on a preview in search results, you can can the a side-by-side view of the change in each file.

Find and replace multiple regexes in all files in a folder recursively

Modify this JavaScript and run using NodeJS.

Sort a list of text

  1. Select all the text to sort
  2. Right click on the selection and click “Sort lines”

Get unique / remove duplicate lines in a list of text

  1. Select the list of text
  2. Click View > Command Paletter
  3. Click one of the unique sort options

Select a column of text

  1. Put the cursor at one corner of the column of text you want to select
  2. On Windows, click and hold “Shift” + “Alt” keys and drag the cursor to the opposite corner of the column of text you want to select
  1. You can then start typing to replace the text or copy the text to the clipboard

Find text in a single file and copy all matches to the clipboard

  1. Open the text in Visual Studio Code
  2. Click Edit > Find
  3. Enter a search term
  4. Click Selection > Select All Occurrences
    All matches will be highlighted. Notice the different highlight color in the two screenshots below.
  1. Right click on the highlighted text and click “Copy”
  1. You could then paste the copied text in a new file

Add text to the beginning of each line of text

  1. Click Edit > Replace
  2. Use the following regex search

Copy differences (diff) between two files

Let’s say you have two files and you want to get only the differences between the two. Visual Studio Code will show you a diff between two files as follows:

  1. Open both files
  2. Select both files in the OPEN EDITORS section
  3. Right click and click “Compare Selected”

You will then see a nice diff between the two files.

But, you won’t be able to copy only the changed lines or export the diff. On Windows, you can use a tool called DiffMerge by SourceGear. If you open both files, you’ll see a similar view.

You can then click Export > File Diffs > Traditional > In Text > To Clipboard to copy the diff to your clipboard.

You can then paste the diff in a text editor like Visual Studio Code and extract only the text you are interested in.

Rename multiple filenames in multiple folders

Let’s say you have many files and you want to rename the filenames all at once. For this, I find it easy to use a tool like Rename Expert. For example, in the screenshot below, I did the following:

  1. Added all files the filenames of which I want to rename
  2. Under “Actions”, chose the actions I want done. In this case, I did 2 actions:
    1. Replace (to replace the first character of each filename with an underscore)
    2. Edit file extension (to change the file extension from php to jpg)
  3. Previewed the changes and resolve any conflicts
  4. Clicked the Apply button to run the batch renaming of files.