My data is in a format I'd like to change
It has this shape:
Header1,
Data1,
Data4
...
Header2,
Data2,
Data5,
...
Header3
Data3
Data6
...
The end goal is to get my delimited data in a common format (CSV or similar), like:
Header1,Header2,Header3
Data1,Data2,Data3
Data4,Data5,Data6
...
The strategy I was thinking of employing was to join 'distant' lines in vscode by alt-clicking distant lines and then using the Ctrl+Shift+P:Join Lines palette command, but that didn't seem to work.
I was wondering if anyone here knew how to join 'distant lines' in vscode
(or if not that, provide an alternate strategy).
Thanks.
If you have any comma's at the end of a line remove it
Make sure you have the same number of lines under each header
Now for every block
Place the cursor at start of line Header2
Now with Ctrl+Alt+DownArrow add multi Cursors or use Shift+Alt+MouseClick at the last line belonging to Header2
Select the whole line: Shift+End
Cut the content: Ctrl+X
With ArrowUp move the cursors up so the first cursor is on the line with Header1
Got to end of line, Add , , Paste Header2 lines: End , Ctrl+V
You can also start with the last block and add it to the previous block, now the cursors can stay on the same line to pick up the content and move it to the end of the previous block.
At the end remove all the empty lines.
Related
As shown in the picture "Before" below, each column separated by comma is not aligned neatedly. Is there any method to align each column vertically like the display effect in Excel?
The effect I wish is shown in the picture "After".
Thanks to #Martin S , I can align the file like the picture "Method_1". As he has mentioned, some characters still cannot align well. I was wondering if this method could be improved?
You can use the TextFX plugin:
TextFX > TextFX Edit > Line up multiple lines by ...
Note: This doesn't work if the file is read only.
http://tomaslind.net/2016/02/18/how-to-align-columns-in-notepad/
Update 2019: Download link from SourceForge
Maybe not exactly what you're looking for, but I recently added a CSV Lint plug-in to Notepad++ which also adds syntax highlighting for csv and fixed width data files, meaning each column gets a different color so it's easier to see.
You can use this python plugin script which utilizes the csv library which takes care of quoted csv and many other variants.
Setup:
Use the plugin manager in Notepad++ to install the "Python script" plugin.
Plugins->Python Script->New Script (name it something like CSVtoTable.py)
Paste the following python script into the new file and save:
CSVtoTable.py
import csv
inputlines = editor.getText().split('\n')
# Get rid of empty lines
inputlines = [line.strip() for line in inputlines if line.strip()]
reader = csv.reader(inputlines, delimiter=',')
csvlist = [line for line in reader]
# transpose to calculate the column widths and create a format string which left aligns each row
t_csvlist = zip(*csvlist)
col_widths = [max([len(x) for x in t_csvlist[y]]) for y in range(len(t_csvlist))]
# To right align - change < to >
fmt_str = ' '.join(['{{:<{0}}}'.format(x) for x in col_widths]) + '\r\n'
text = []
for line in csvlist:
text.append(fmt_str.format(*line))
# open a new document and put the results in there.
notepad.new()
editor.addText(''.join(text))
Open your CSV file in notepad++
Click on Plugins->Python Script->Scripts->(The name you used in step 2)
A new tab with the formatted data should open.
Update (right aligned numbers & left aligned strings):
Use the following python script if you want to right align number fields from the CSV - it looks at the second line of the csv to determine the types of the fields.
import csv
import re
num_re = re.compile('[-\+]?\d+(\.\d+)?')
inputlines = editor.getText().split('\n')
# Get rid of empty lines
inputlines = [line.strip() for line in inputlines if line.strip()]
reader = csv.reader(inputlines, delimiter=',')
csvlist = [line for line in reader]
# Transpose to calculate the column widths and create a format string which left aligns each row
t_csvlist = zip(*csvlist)
col_widths = [max([len(x) for x in t_csvlist[y]]) for y in range(len(t_csvlist))]
# Numbers get right aligned
type_eval_line = csvlist[1 if len(csvlist)>1 else 0]
alignment = ['>' if num_re.match(item) else '<' for item in type_eval_line]
# Compute the format string
fmt_str = ' '.join(['{{:{0}{1}}}'.format(a,x) for x,a in zip(col_widths,alignment)]) + '\r\n'
text = []
for line in csvlist:
text.append(fmt_str.format(*line))
# open a new document and put the results in there.
notepad.new()
editor.addText(''.join(text))
Notepad++ CSVLint
Install CSVLint Plugin
Open CSV file. Or manually set Language > CSVLint. This will give you nicely colored output.
To reformat do this:
Open lower pane: Plugins > CSV Lint > CSV Lint Window.
Click the Reformat button. Check the box Align vertically (not recommended). -- This may screw up your data, so think twice before clicking OK.
Reformatted output:
If you want to try this yourself: Here is my sample input:
TIMESTAMP_START,TIMESTAMP_END,TA_ERA,TA_ERA_NIGHT,TA_ERA_NIGHT_SD,TA_ERA_DAY,DA_ERA_DAY_SD,SW_IN_ERA,HH,DD,WW-YY,SW_IN_F,HH
19890101,19890107,3.436,1.509,2.165,6.134,2.889,100.233,283.946,1.373,99.852,2.748,1.188
19890108,19890114,3.814,2.446,2.014,5.728,2.526,91.708,286.451,1.575,100,100.841,0.742
You could use Search&Replace to change all occurrences of , to ,\t. This will add a tab after each ,.
This method has however some drawbacks:
you effectively add white-space characters to your document (in case you need to edit and save it).
This works well only if the difference (in terms of number of characters) between the longest and the shortest numbers is less than 1 tab-size (usually 4 characters).
So I've got two separate blocks (on the same document), one with keys:
one
two
three
Another one with values:
First
Second
Third
And I want to join them into a PHP array:
$arr = [
'one' => 'First',
'two' => 'Second',
'three' => 'Third',
],
Or a Javascript/JSON object (So I make this question more googlers-friendly :D):
var obj = {
one: 'First',
two: 'Second',
three: 'Third'
};
Ideas using cool Sublime Text shortcuts?
Got it! I figured out after placing the question so here it goes.
For PHP arrays:
Command+A to select all.
Command+Shift+L to place a cursor at the end of each line.
' to wrap each line on single quotes.
Select all the "value" lines.
Command+Shift+L to place a cursor at the end of each line.
Command+X to cut all value lines. (NOTE: cutting them as multiple cursor selection cuts them as 'separate chunks').
Select all the "key" lines.
Command+Shift+L to place a cursor at the end of each line.
Right to remove individual selections.
Type => to add the PHP array arrows.
Command+V to paste.
Type , to add the end of the line comma.
Command+A to select all.
Hit [ to wrap the whole selection in brackets.
For Javascript/JSON:
Select all the "value" lines.
Command+Shift+L to place a cursor at the end of each line.
' to wrap each line on single quotes.
Select all the "value" lines again.
Command+Shift+L to place a cursor at the end of each line.
Command+X to cut all value lines. (NOTE: cutting them as multiple cursor selection cuts them as 'separate chunks').
Select all the "key" lines.
Command+Shift+L to place a cursor at the end of each line.
Right to remove individual selections.
Type : to add the PHP array arrows.
Command+V to paste.
Type , to add the end of the line comma.
Remove the last line comma.
Command+A to select all.
Hit [ to wrap the whole selection in brackets.
PC users, feel welcomed to to add PC shortcuts!
I am using Windows 8 OS
I have some projects where I repeatedly add the same tags to different types of elements, but the format of how the elements are presented through code always stays the same. I'm looking for shortcuts that will help me do these tasks quickly. Is there a shortcut that lets you add the same tag for multiple lines that you specify? I know you can do (CTR + F3) To select clone tags and change all of them, but I want to add tags to elements that previously had no tag. Is there a way you can make your own shortcuts for your tags, like if I type in "li" It will automatically put in "" and all I have to do is hit enter?
Here is an example of the elements and tags I added:
<ul>
<li type="square">Extra Grip
<li type="square">Made of Titanium
<li type="square">Built in Selsoft Processor
<li type="square">Portable</ul>
<b>MBS:</b> 44 kN (10000 lbf)<br>
<b>Weight:</b> 1 lbs 13.2 oz (828 g)<br>
<b>Length:</b> 14.40" (36.6 cm)<br>
<b>Width:</b> 3.75" (9.5 cm)<br>
<b>Height:</b> 1.00" (2.5 cm)<br>
<b>Material:</b> Titanium
Ctrl+C, Ctrl+X and Ctrl+V let you copy/cut/paste lines if you don't select anything. So, Ctrl+X doesn't "delete" a line, it cuts it. To delete a line, the default shortcut is Ctrl+Shift+K :)
Highlighting a bunch of lines then hitting Cmd (Ctrl?) +Shift+L gives you multi-cursors on each line. I do that, followed by Cmd + Right arrow (End?) to get easily get a cursor at the end of a series of lines to simultaneously type something after each.
Ctrl+Shift+J expands the selection to the indentation level of the current line. So if you want to select a block of code with the same indentation it's really useful.
Alt + F3 select all occurrences of current word for multiple editing. Very useful.
A few written about in more detail: http://whiletruecode.com/post/7-handy-text-manipulation-tricks-sublime-text-2
Have you tried to make your own snippets? It may not be exactly what you are asking for, but could be another way to do it.
Try the New Snippet command in the Tools-menu and add the following and save it:
<snippet>
<content><![CDATA[
<li type="square">${1:Item} ${2:}
]]></content>
<tabTrigger>li</tabTrigger>
</snippet>
This will enter an <li>-tag in the current file if you type li and then press Tab.
You can also add a <scope> tag to limit it to HTML-files.
I have a text file with 100's of news articles.
I need to Select All > Take cursor to the beginning of each line and have the 'multiple cursors' open so I can add some data.
Since the new articles do not have the same begging character, I can not use CTRL+F3.
Is there a way to [CTRL] + [A] (Select All) then > Go to the begging of each line with 'multiple cursors' open ?
You should select all with ctrl+A.
Then split the selection into one selection per line with ctrl+shift+L.
Then press the left arrow key.
Now you should be able to type data at the beginning of each line.
Let me add some answer, ( work in sublime 2 / sublime 3 )
i try with #Riccardo Marotti step
, but when the article have tab it will lead to the very far first line
So I make some fix , To make cursor in every front line or every end of line :
ctrl+A // select all
ctrl+Shift+L // Add cursor to all line
Shift+Home // put cursor to first word in the line
to get the end of line no need to do the first step again if you are in the first line, just do this one :
Shift+End // put cursor to end of line, then
click "right arrow" to remove the drag
or you can try this one (really an experimental works)
Just use you center of mouse, click it (the center roll of your
mouse), then drag, this will lead to add more cursor
, then just press left/right
or try to seek more here :
sublime-text-multiple-cursor-shortcut
override-shortcut-for-multiple-cursors
hope this help
I think ctrl+alt+down (or up) when your cursor is at the beginning of a line might be what you're looking for. It will put the cursor on multiple lines, and what you type will be duplicated on each.
A client has requested a specific CSV format for their report. When exported to CSV, blank cells are being added to the group header.
The comma's in the dollar values are also being seen as delimiter, so I need to figure out a way to add quotes around them. I have tried ToText, but the formula check keeps stating that the remaining text does not appear to be part of the formula.
Here is the formula
if{bnkacrpt.trans_type}<>"OB"
then if {bnkacrpt.amount} > 0
then {bnkacrpt.amount}
else if {bnkacrpt.amount} < 0
then ({bnkacrpt.amount} * -1)
else 0
else 0
The layout (This is the only active component in the report):
The export options:
The result:
What they want:
I have tried many different variations on the export options, but am having no luck. Any help is much appreciated!
Sorry this isn't a complete answer, I would comment if I could.
Totext needs parenthesis around the text you're converting.
Try this to at least get the formula working.
if {bnkacrpt.trans_type} <> "OB"
then if {bnkacrpt.amount} > 0
then ToText({bnkacrpt.amount})
else if {bnkacrpt.amount} < 0
then ToText({bnkacrpt.amount} * -1)
else ToText(0)
else ToText(0)
OK. So I had to basically hack the living daylights out of it and just make it work.
Formula (there is another formula for the description column that I did similar stuff to):
stringVar amount := totext({bnkacrpt.amount});
stringVar lessamount := totext({bnkacrpt.amount} * -1);
if{bnkacrpt.trans_type}<>"OB"
then if amount > totext(0)
then '"'+amount+'",'
else if amount < totext(0)
then '"'+lessamount+'",'
else totext(0)+','
else totext(0)+',';
In the Layout as shown in my original post, I added a single comma to the blank fields in between the data fields on the description line.
The export options:
The result:
I just need to figure out how to get rid of the empty line on top and add empty lines in between the results.
So. Close.
I also hate this client very much now.
EDIT
Finally got it right! Well, from my point of view anyways.
To add the blank lines between the details results, and under the headers line, I added a formula only with "ChrW(13)" in it and put it at the end of both lines.
/*****************************************/
To remove the most annoying blank line at the top of the resulting CSV file, I went through all the headers that were suppressed above using the section expert and ticked "Hide", 'Suppress' and Suppress blank section. Not sure which was the culprit, or it could have been all of them, but it works, so I don't care :)