How to conditionally replace numbers using vim - vim

I can see a lot of discussion on how to replace strings conditionally using :%s. But I want to do exactly this in my file that has a huge bunch of numbers in a CSV format:
Find numbers < -100
Replace them by -98
How can I do this in VIM or any other editor/script language?

It is possible using the submatch() function like this:
:%s/-[0-9]\+/\=submatch(0) < -100 ? -98 : submatch(0)/g
Now every number smaller than -100 will be replaced by -98 and the rest just stays the same. Note that this regex will only match negative numbers.

Related

Calculating all number from a text file (division)

I have a file or a text which contains huge numbers. This is how it looks:
2622256647732477952, 3146707977278973440, 3776049572734768128, 4531259487281721344, 5437511384738065408, 6525013661685678080, 7830016394022813696, 9396019672827375616, 11275223607392849920, 13530268328871419904,
I want to divide every number by the factor of 100. Is there any fast way to do this? notepadd++ maybe? or any 3rd party editor which is able to do such stuff?
It's around 1000 numbers would be pretty time consuming to do this manually.
All the numbers seem to be integers. If that is true, and if they are all above 100 (the divisor), why not just use a regular expression to insert a decimal point in every number.
In Notepad++ try:
Search string: (\d+)(\d{2})
Replace string: $1.$2
Check "Regular expression" box and hit "Replace all".
Edit:
In the special case you mention in your comment, where the decimals should just be disregarded, you can simply use (\d+)\d{2} as search string and $1 as the replace string. Note that the result won't be rounded to the nearest integer though (11189 should become 112 really, but you'll get 111).
Other options include importing the string into Excel or other spreadsheet software and use a formula in there, writing a javascript snippet to split the string up and divide each number etc.

Format numbers in thousands (K) in Excel

In MS Excel, I would like to format a number in order to show only thousands and with 'K' in from of it,
so the number 123000 will be displayed in the cell as 123K
It is easy to format to show only thousands (123), but I'd like to add the K symbol in case the number is > 1000.
so one cell with number 123 will display 123
one cell with 123000 will show 123K
Any idea how the format Cell -> custom filters can be used?
Thanks!
Custom format
[>=1000]#,##0,"K";0
will give you:
Note the comma between the zero and the "K". To display millions or billions, use two or three commas instead.
Non-Americans take note! If you use Excel with "." as 1000 separator, you need to replace the "," with a "." in the formula, such as:
[>=1000]€ #.##0." K";[<=-1000]-€ #.##0." K";0
The code above will display € 62.123 as "€ 62 K".
[>=1000]#,##0,"K";[<=-1000]-#,##0,"K";0
teylyn's answer is great. This just adds negatives beyond -1000 following the same format.
Enter this in the custom number format field:
[>=1000]#,##0,"K€";0"€"
What that means is that if the number is greater than 1,000, display at least one digit (indicated by the zero), but no digits after the thousands place, indicated by nothing coming after the comma. Then you follow the whole thing with the string "K".
Edited to add comma and euro.
The examples above use a 'K' an uppercase k used to represent kilo or 1000. According to wiki, kilo or 1000's should be represented in lower case. So, rather than £300K, use £300k or in a code example :-
[>=1000]£#,##0,"k";[red][<=-1000]-£#,##0,"k";0
I've found the following combination that works fine for positive and negative numbers (43787200020 is transformed to 43.787.200,02 K)
[>=1000] #.##0,#0. "K";#.##0,#0. "K"

Concatenate a special character to a column of data?

9
37
92
93
96
98
118
128
135
136
139
I have about 13K plus records like the list above. And I want to append a ',' after every number?
What would be the best/easiest way to do this?
This sounds like a job for Notepad++!
Hit Ctrl+F and choose the Replace tab, then fill in these details:
Find what: \r\n
Replace with: ,\r\n
Search mode: Extended
Click Replace All, and Bob's your uncle!
Depending on your os and language:
Read a linw
Write all but the carriage return
Write ,
Write the carriage return
carry on at 1. unless you have finished.
On Unix/Linux boxes you could use sed or awk, just about any programming language, I wouldn't recommend asm or fortran but not impossible with them.
On any machine any language available that supports text processing, any utility that supports regular expressions. In excel you can create another column that concatenated the contents of the cell to the left and ',' then hide the original column
for data in column A1 you can use below
CONCAT(A1,CHAR(39), char(44),CHAR(39))
FYI,
CHAR(39) - '
CHAR(44) - ,

Generating a list of all strings possible using 6 characters

I need to generate a list that contains all possible 4 character strings made up of the characters A,B,C,D,E,F. What is the best way to accomplish this? Characters can be used any number of times.
list("".join(l) for l in itertools.product('ABCDEF', repeat=4))

Replace colon with tab to make columns

I have word lists where the word or expression in Spanish is separated by its translation with a colon (":"). I want to make two columns, one for Spanish, the other for English. In vim I tried with
:%s/:/^I^I^I/g
But it does not give the desired output. The different columns are not aligned.
When deleting the colon by hand and inserting the number of tabs with the same amount of tab strokes, it always ends up aligned.
Any idea how to solve this, preferably in vim?
On a Linux/*nix system I use column(1)
:%!column -s':' -t
followed by
:%retab!
I'd probable do a
:s/:/^I/g
followed by a
:set ts=25
Where 25 is the length of the longest word expected + 2. So, if you expect the longest word of your input (on the left side of the colon) to be 12 characters, I'd choose something around 14 instead.
With Tabular.vim it's quite easy, just type :Tab /:\zs and it does the rest.
When in insert mode a setting of yours makes tab to reach a nth column. This is set with the 'softtabstop' settings if I am right.
For those tasks you could use a plugin like Align.vim or Tabularize.
Another option is to insert a huge number of spaces, and then use Visual Block with < operator as many times as necessary, if you have to do it once. Otherwise prefer reusable methods.

Resources