I have a fixed width flat file with n columns. I need to add a new column at the end that is a concatenation of some of the columns.
Eg.
0624 11011 LOCA
0624 11031 LOCC
0624 11041 LOCB
turns into
0624 11011 LOCA 0624LOCA-ABC
0624 11031 LOCC 0624LOCC-ABC
0624 11041 LOCB 0624LOCB-ABC
I imported file files as text in Excel and was able to generate the column quickly using the concatenate function.
But I now need to generate it as the flat file again. How do I do this?
Is there a feature in Excel to allow me to generate the sheet as a flat file with spaces. Or can I "transfer" the column to the same file in Notepad++.
I am looking for a handy keyboard option, else I have to go to SQL.
With Npp, I'd do:
Ctrl+H
Find what: ((\S+)\s+\S+\s+(\S+))
Repace with: $1 $2$3-ABC
Replace all
Make sure you have checked Regular Expression.
I assume that the delimiter is one space.
In Excel, click Save as, and choose the "save as type:" "Formatted Text (space delimited)".
Alternative, in Notepad++, if they are fixed width to start with, use Column Copy/Paste to build the extra column. (Hold Left ALT down while highlighting 1 column, CTRL+C then put cursor at end of first line, add a few spaces, click CTRL+V. Repeat for 2nd column ;)
If you need to add additional text to all columns (ie hyphen seperating them), Use the Left ALT trick again to highlight a zero width selection across all rows, then just type .. it'll mirror the typing for all rows :)
If you're file is significantly larger, however, I'd stick with Excel, or try Toto's Regular Expression replace ;)
Related
I am trying to enter approximately 190 txt datafiles in Excel using the New Query tool (Data->New Query->From File->From Folder). In the Windows explorer the data are properly ordered: the first being 0summary, the second 30summary etc.
However, when entering them through the query tool the files are sorted as shown in the picture (see line 9 for example, you will see that the file is not in the right position):
The files are sorted based on the first digit instead of the value represented. Is there a solution to this issue? I have tried putting space between the number and the summary but it also didn't work. I saw online that Excel doesn't recognize the text within "" or after /, but I am not allowed to save the text files with those symbols in their name in Windows. Even when removed the word summary the problem didn't fix. Any suggestions?
If all your names include the word Summary:
You can add a column "Extract" / "Text before delimiter" enter "Summary", change the column type to Number and sort over that column
If the only numbers are those you wish to sort on, you can
add a custom column with just the numbers
Change the data type to whole number
sort on that.
The formula for the custom column:
Text.Select([Name],{"0".."9"})
If the alpha portion varies, and you need to sort on that also, you can do something similar adding another column for the alpha portion, and sorting on that.
If there might be digits after the leading digits upon which you want to sort, then use the following formula for the added column which will extract only the digits at the beginning of the file name:
=Text.Middle([Name],0,Text.PositionOfAny([Name],{"A".."z"}))
Here is an example cells
AED+FGH
AACDV+GFH
AFGH+ FGH
SDF+ GH
ADFG+ SDDF
HGJ+ HJ
If I want to separate the letters before/after the '+' - how do I do this?
Thank you.
There's Text to Columns, using + as a delimiter. That's probably the quickest way.
Select the data, then go to Data --> Text to Columns. Choose "Delimited", using a + delimiter. If you want, choose a destination (that's optional, you can just click "Finish" which will overwrite the current data).
Alternatively, you can use LEFT() and RIGHT().
To get parts left of the +:
LEFT(A1,SEARCH("+",A1)-1)
and right of the +:
RIGHT(A1,LEN(A1)-SEARCH("+",A1))
My question is very simple:
Is there a shortcut in Sublime Text 3 which allows to remove blank lines in a selected text? (same for blanks in a line)
For instance, how to make this text:
a
b
c
To become:
a
b
c
And this line:
I need to remove these blanks.
to become this line:
Ineedtoremovetheseblanks.
You don't need a plugin to do this, a simple regex search and replace will do. First, select the text where you'd like to delete the blank lines. Then, select Find → Replace… (or, hit CtrlH on Windows/Linux, ⌘⌥F on OS X). Make sure the "Regular Expression" and "In selection" buttons are selected:
In Find What:, enter ^\n, and make sure the Replace With: field is empty. Then, simply hit "Replace All" and this:
becomes this:
As a bit of explanation, the regular expression ^\n searches for the beginning of a line (^) immediately followed by a newline character (\n). If you suspect that some of your "blank" lines contain whitespace, like space or tab characters, you can use ^\s*\n instead - \s* matches 0 or more whitespace characters, including newline characters.
For your second example, use the same Find/Replace settings as above, except your regular expression should simply be \s*.
There is a package called Trimmer. You can install it via Package control.
After you got the package you can use its functionalities. Just highlight the text you want to change (or don't select anything if you want to change the entire file) and then choose:
1) Edit > Line > Delete Empty Lines
2) Edit > Line > Remove Blank Spaces
Alternatively, as Chris's answer already pointed out you can use the classic search & replace functionality that is already present in the editor.
AS my original answer was incorrect, I have found this package which will be suitable for your needs.
The package is called DeleteBlankLines, for Sublime Text 3, has the ability to delete blank lines for the entire document and within the selected text only, all from a key stroke just like you were after.
The package can be found here:
https://packagecontrol.io/packages/DeleteBlankLines
I prefer using \s+, which will get one or more (i.e., the + character) whitespace characters (i.e., the \s escape sequence).
The * character will do zero or more, which on some versions can break up the individual words into letters.
I've got a long list of codes and text in the same cell. I need to return all the text from the right BUT the first five characters
ex. V2341 something here
I need to return the something here
=RIGHT(LEN(A1)-5)
That's what I've tried, but no luck with it. I know it's gotta be close to correct, what am I missing?
Thanks.
You're looking for MID.
If column A contains
A C D
==================== ====================== ==============
1 ABCDE12345 D1=MID(A1, 6, LEN(A1)) 12345
2 ABCDE1234567890ABCD D2=MID(A2, 6, LEN(A2)) 1234567890ABCD
REPLACE function can also be used to replace a designated number of characters with nothing, i.e.
=REPLACE(A1,1,5,"")
You can also select the whole column, use ctrl+f, then find and replace the 'v' with nothing. This will work easily if you have the same first amounts you are trying to get rid of.
If you are trying to get rid of the same number of characters, but they are different characters, you can go to Data -> Text to Columns (in Data Tools Sections). Then select the radio button fixed width. Click Next. Move the line in the graphic to the position you want it in, click Finish.
I have a bunch of lines that I'd like to split into two columns, and get the data from each column. The data looks something like this:
current_well.well_number
current_well.well_name
current_well.well_type_code
well_location.section
well_location.range
Essentially what I'd like to do is split the line based off of the period, turn the data into two columns, and then grab the data for each column. I know this can be done in Excel, but I'm really interested in VIs solution for this problem. I know that
%s/\./
will format the string with empty spaces. But once I have the data looking like:
current_well well_number
current_well well_name
current_well well_type_code
well_location section
well_location range
How do I grab all the values for each column so I can paste it into another application?
The linux column command works well for creating the columns
:%!column -s . -t
Then use block copy.
One option is to use this Align plugin to line up the periods so you can more easily select a column in Visual Block mode. e.g. if you do this:
:%Align \.
You'll end up with:
current_well . well_number
current_well . well_name
current_well . well_type_code
well_location . section
well_location . range
If you don't want to use a plugin, try padding your columns with spaces. e.g. for your text:
:%s/\v(.*)\.(.*)/\=printf("%-16s %s", submatch(1), submatch(2))/
That'll leave you with:
current_well well_number
current_well well_name
current_well well_type_code
well_location section
well_location range
Then you can Ctrl-V and select a column of text to copy. Just make sure you pick a column width wider than your widest value.
I've run into this problem a number of times. Changing the dots to tabs will line them up mostly nice, but not really. It's true that all the columns will start on a tabstop, but there's no guarantee that they'll be on the same tabstop. For instance, if this was your original text:
lt1tab.value1
gt1tabstop.value2
and you do:
%s/\./\t/g
and assuming a tabstop is 8 spaces, you'll get:
lt1tab value1
gt1tabstop value2
What you might want to do instead is remove everything but the last column (or whichever column you want). For instance:
%s/^.*\.//
will leave you with:
value1
value2
Which you can easily copy and paste.
If you don't have to use Vim, you can use unix's cut to do what you want:
cut -f2 -d. input_file > output_file
I do this all the time, simply by padding with a lot of space:
%s/./ /
Then <c-v> to enter block mode in the empty space, draw the column position I want, < to shift the text to the right toward my column, then hold down .. Takes seconds.
Another way you can do it if you know the specific column position you want to line up on: go to the first line and start recording (qq), find your delimeter (f.), insert a bunch of space (20i <esc>), delete space back to a specific column (d15|), then move down a line (j). Then you just hold down shift and roll your fingers across #Q a bunch of times (or use a count 10#q) until all the columns are lined up. :)
Obviously, the above methods with block select and/or Excel are easier. But I'm a masochist and I decided to assume you didn't have access to either of those and try to do it with only vi commands. Here's the horrible thing I thought up:
My basic plan is to turn the list of joined columns into two lists, one after the other. This basically involves breaking each row into two lines, then copying every other line to the end of the file.
So, first we have to break each line into two lines with this command:
:%s/\./^M/
Next, swing by the bottom of the file and create an empty line, then return to the first line. This will help with readability later.
Go[Esc]
:1
Now, you need to map the following sequence to your favorite key:
:map [Key] mkjddGp'kj
(For the record, this marks the current row, deletes the row below it, pastes that row at the bottom of the file, returns to the row you started from, and then moves to the next row.)
Finally, press the mapped key once for every row in your list. So, with the example list, you'd press it 5 times. Make sure you start from the first line in the list!!
What you'll have at the end is the following:
current_well
current_well
current_well
well_location
well_location
well_number
well_name
well_type_code
section
range
You can now easily copy each list to wherever you need to put them.
Ctrl V selects a column
Why not block visual mode ? C-v, select what you want, and paste it in some other application.
For that kind of pasting, you should probably set
:set guioptions+=a
as well.
Hmm, now that I've re-read your question. Are you asking how to divide the data into columns, or how to paste it in some other app ?
I think I got it now (3rd reading).
Do as you've done and get the spaces where the dots used to be. Then do another substitution, and replace the spaces with tabs, each tab being, 15 or something. It will line them up nicely.
Then you can select and copy them to wherever.
You can also match the regex to select the second column for example, but although it is light up like a christmas tree, you won't be able to yank it.
I'd convert the dots to tabs, then block select each column:
:%s/\./CtrlV-Tab/g
CtrlV to then block select the columns.
Or, as you'd have tab separated values, they will paste directly into columns in Excel.
At the top of the file:
!}awk -F. '{print $1}' >> %
You will be told the file has changed. Select the L (load) option.
Add a blank line under the original lines, go back to the top.
for(i=1;i<=NR;i++);print $2 >> %
Again select the L option
Delete the original lines.
You now have the first and second parts in two groups.
One possible solution:
w (or W for WORDS) jumps to next word,
CTRL-R CTRL-W gets the word under cursor and pastes it into command-line
Now you have to chain those commands in a loop and then output them in your own manner.
You can select columns in gvim by typing either Ctrl-VCtrl-G or Ctrl-QCtrl-G (depending on if you are using windows insert key mappings) and then selecting with the mouse.
nnoremap <BS> v"by:%!column -t -s<C-r>b -o' '<CR>
This is a nice solution following on from #NES & #SergioAraujo idea. Put it in your vimrc.
USAGE. Move the cursor to the delimiter. Then hit Backspace.
Explanation we use the named register b to hold our delimiter. Then substitute it as the variable for the --separator option of the column UNIX utility.
And Backspace is a good key because in normal mode is the same as h. But then again you might also be prone to hitting it accidentally.