Windows tip: how to efficiently filter and save data records with a script
Guide

Windows tip: how to efficiently filter and save data records with a script

Florian Bodoky
25.3.2024
Translation: Patrik Stainbrook

Filter out individual records from databases and save them in a separate file. No more miles of Excel commands and copy-paste marathons in the console.

An acquaintance of mine manages the membership database of a large gymnastics and shooting club in his free time. Since the introduction of the revised Data Protection Act, he’s no longer allowed to circulate the entire Excel list, but only individual, partially anonymised data records.

  • Background information

    The new Swiss data protection law – what you need to know

    by Florian Bodoky

To create lists for car pools, for example, he has to filter in Excel and laboriously copy and paste data records into other files. Quite a bit of work.

Automate queries in PowerShell

There’s an easier way – a PowerShell script. It’ll search for the corresponding parameters in a database, read the respective data records and copy them into a new, separate text file. This is only possible if the database is in .txt, .ini or .csv format. You can save files in .xlsx to .csv as a copy.

Here’s how to build your script:

  1. Open the folder where you saved the CSV file (or TXT/ini file).
  1. Open PowerShell by clicking on a free space in the folder and selecting «Open in Terminal».
In the folder where you saved the file, right-click to open the context menu.
In the folder where you saved the file, right-click to open the context menu.
Source: Florian Bodoky
  1. Type the following parameters into the text field and press Enter: get-Content '.\contacts.csv' | Select-String -pattern "Winterthur" | Out-File resultat.txt. The first part of the command selects the text file in question. The second selects the data records that fulfil your conditions. Number three then saves the search results in a text file. In my file, all data records containing the parameter Winterthur are now selected. You can replace the search term with any of your choosing.
This is what the command should look like.
This is what the command should look like.
Source: Florian Bodoky
If a red entry appears, there’s probably a typo somewhere.
If a red entry appears, there’s probably a typo somewhere.
Source: Florian Bodoky
  1. The file will now be created in the same directory as your database.

You now have a text file containing the corresponding data records – make sure to change the file name, in my case «contacts.txt» and the pattern, in my case «Winterthur».

Note: This tip and command refer to a single use case. Do you have any similar problems that I could help you with? Drop them in the comments! I’ll try to answer them in other Windows tips

15 people like this article


User Avatar
User Avatar

I've been tinkering with digital networks ever since I found out how to activate both telephone channels on the ISDN card for greater bandwidth. As for the analogue variety, I've been doing that since I learned to talk. Though Winterthur is my adoptive home city, my heart still bleeds red and blue. 


These articles might also interest you

Comments

Avatar