I have a file with lines of text that looks like this:
category1.name1
name2
I want the output to look like this:
category1 name1
name2
I’m editing this file in VS Code on a Mac. I’m happy to use regex in VS Code, or a command line solution.
I can replace the "." with a tab easily enough, but how do I add tabs to the start of the line if there is no "." in the line?
2
Answers
Here’s a command-line solution using
awk
:You can adjust the width of the first field (the
%-15s
in theprintf
) to something more appropriate as necessary, but otherwise, this seems to fill in what you’re looking for.For example:
If your heart’s set on using tabs, you can use the following modified version, but if the first field is longer than a tab character’s width (e.g., eight), things won’t line up:
Here is another way to do this. I am not sure how uniform your data is try this.
Using an extension I wrote, Find and Transform, you can use javascript in a keybinding to get the lengths of the words you need. Demo:
That uses this keybinding:
This uses spaces to align the lines, much easier than tabs. If it is absolutely necessary to use tabs let me know and I’ll see if anything can be done to change this.