How can I (if possible) get a count of total number of occurrences, in my Rscript, of a search, when searching for it using the "Find/Replace" feature in rstudio?
For example, say I had the following script:
a <- c(1,2,3)
print(a)
print("Are you there?")
Then when going Ctrl + F (or commands + F on mac), and typing a (lowercase), somewhere it would say 2.
I am running version 3.5.1 on MAC.
The following image should help clarify the feature I am talking about,
One way is to Find and Replace with an identical string. If you find a and replace all with a it won't change your code, but will say "2 occurrences replaced".
Note that you need to check Match case, otherwise A will also be matched (and replaced with a)
I don't know a way to achieve this in RStudio, however you can convert your code to a string and then search the string using the stringr package.
Using your example:
my.code = ' a <- c(1,2,3)
print(a)
print("Are you there?") '
> my.code
[1] " a <- c(1,2,3)\n print(a)\n print(\"Are you there?\") "
Now to search for lowercase "a":
library(stringr)
str_count(my.code, "a")
[1] 2
Related
How do I add a line-break/new-line in IRB/Ruby? The book I'm learning from shows this code:
print "2+3 is equal to "
print 2 + 3
without telling how to go to the second line without hitting Enter, which obviously just runs the program.
You could use semicolon at the end of statement like this puts "hello";puts"world"
That book might be taking very tiny steps to introducing this idea:
print "Continues..."
puts "(Up to here)"
The print function just outputs to the terminal exactly what it's given. The puts function does the same but also adds a newline, which is what you want.
The more Ruby way of doing this is either:
puts "2+3 equals #{2+3}" # Using string interpolation
puts "2+3 equals %d" % (2 + 3) # Using sprintf-style interpolation
Now if you're using irb, that's a Read-Evaluate-Print-Loop (REPL) which means it executes everything you type in as soon as you press enter, by design. If you want to use your original code, you need to force it on one line:
print "2+3 equals "; print 2+3
Then that will work as expected. The ; line separator is rarely used in Ruby, most style guides encourage you to split things up onto multiple lines, but if you do need to do a one-liner, this is how.
When writing code in, say a .rb file the return key is just used for formatting and doesn't execute any code.
You can put a semicolon after the first line, like this:
print "2+3 is equal to ";
print 2 + 3
I have a very large expression which contains many fractional numbers (a/b). I want to replace all fractional numbers by their latex form i.e. my output would be \frac{a}{b} globally. Here is a part of the input file:
+ 93
- 220/3*zeta3
+ 536/9*zeta2
- 4/5*zeta2^2
My output would be something like :
+ 93
- \frac{220}{3}*zeta3
+ \frac{536}{9}*zeta2
- \frac{4}{5}*zeta2^2
I can do it manually by vim editor (which is very time consuming). I was looking for a script which can do it globally for all such fractions. Is it possible to do in shell script?
With GNU sed:
sed -E 's|([0-9]+)/([0-9]+)|\\frac{\1}{\2}|' file
Output:
+ 93
- \frac{220}{3}*zeta3
+ \frac{536}{9}*zeta2
- \frac{4}{5}*zeta2^2
Assuming your fractions consist only of numbers, and they don't span multiple lines:
:%s!\v(\d+)\s*/\s*(\d+)!\\frac{\1}{\2}!g
using regex :
match : ^(\s+(\+|\-)\s+)(\d+)\/(\d+)(\*.*)$
replace : \1\\frac{\3}{\4}\5
use tools such as sed to edit file.
see DEMO
Usually, when you think:
I can do it manually by vim editor (which is very time consuming).
You're doing something wrong, since nothing should take long in the vim editor, thus:
Use a vim macro
First search for /:
<esc>/\/
Record the following macro:
nbifrac{<esc>nvc}{<esc>ea}<esc>
Step by step
Start recording a macro in buffer q:
qq
Find next occurrence of /
n
Step in front of numerator and insert frac{:
bifrac{<esc>
Find the division sign, select it and change it to }{:
nvc}{<esc>
Step over denominator and append }:
ea}<esc>
Stop recording macro:
q
Play the macro 1000 times if you like:
1000#q
Configuration
Windows 7 Service Pack 1 (64-bit)
VIM 7.4 (2013 Aug 10), 32-Bit GUI version
cscope_macros.vim plugin from www.vim.org, version 2.0.0
The problem
The plugin maps several cscope find functions to open in horizontal or vertical splits using 'CTRL-spacebar' or <CTRL-#>, as this is how VIM recognises it according to the plugin documentation. here is a snippet from the plugin:
" Using 'CTRL-spacebar' (intepreted as CTRL-# by vim) then a search type
" makes the vim window split horizontally, with search result displayed in
" the new window.
"
" (Note: earlier versions of vim may not have the :scs command, but it
" can be simulated roughly via:
" nmap <C-#>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-#>s :scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-#>g :scs find g <C-R>=expand("<cword>")<CR><CR>
However, 'CTRL-spacebar' does not work. When I look at what has been mapped <C-#> is actually translated as <nul>. For example, if I use the command :map, this is the result for the cscope plugin mapped keys.
n <nul>d :scs find d <C-R>=expand("<cword>")<CR><CR><Tab>
n <nul>i :scs find i <C-R>=expand("<cfile>")<CR><CR><Tab>
n <nul>f :scs find f <C-R>=expand("<cfile>")<CR><CR><Tab>
n <nul>e :scs find e <C-R>=expand("<cword>")<CR><CR><Tab>
The only thing I can find 'CTRL-spacebar'/<CTRL-#> (:help index) is
tag char action in Insert mode ~
-----------------------------------------------------------------------
i_CTRL-# CTRL-# insert previously inserted text and stop
insert
But this is not the behaviour I observe.
When I try using 'CTRL-spacebar' in insert mode all that happens is that a space is inserted at the cursor. When I use it in normal mode it seems to move the cursor to the beginning of the next word, or the next line if it is blank.
So, how do I map 'CTRL-spacebar' in VIM on windows?
In Windows GVIM, use the straightforward <C-Space> as the left-hand side of the mapping. <C-#> or the equivalent <Nul> is a workaround for the (Linux) terminal, which in general offers fewer mappable keys. The instructions were presumably aimed at that only.
I have a 900mb log file which I can open in SublimeText 3. This file is bloated with lines similar to the following.
10/08/2014 23:45:31:828,Information,,,,ExportManager: ,No records to send and/or not connected
How can I filter out all the lines which contain No records to send and/or not connected
You can do a regular expression search-and-replace:
Click Find > Replace.
Ensure that the Regular Expression button is pressed.
For the Find What field, put:
^.*No records to send and/or not connected.*\n
Leave the Replace With field empty.
Click Replace All
For people that don't want to write a regex - you can just select the search string, hit ctrl+cmd+g or pick "Quick Find All" from the menu, which will get you selections for each matching string; from there Home will move every selection cursor to the start of the line, shift+End will select every matching line, and del, del will delete all of them.
Multiple cursor editing is fun!
i could not get the regex to work so I used Alt-F3 approach from this answer:
https://superuser.com/questions/452189/how-can-i-filter-a-file-for-lines-containing-a-string-in-sublime-text-2/598999#598999
Select string of interest
Hit Alt+F3 to go into multi-cursor mode on all occurrences (Ctrl+CMD+G on Mac OS X)
Hit Ctrl+L [see comments] (Cmd+L on Mac)
Copy-paste selection to another buffer
Del
This is what i found for the windows users:
Select the string (every line containing this string is to be removed).
Press ALT+F3 .
Press Ctrl+L .
Press Delete .
Neither of the regex code suggested above worked in my case, but this did work:
.*(text in question).*
A simple way of doing it is:
1 Open Sublime Text
2 Find => Replace (Ctrl + H)
3 in Find write the desired text
4 click Find All
5 press ctrl + shift + K to remove all the lines where this search is present
This is a quick solution to remove some lines that contains some text
Above answers are the correct ways, but if you want to get rid of the rows with even a single string then do,
Find -> Replace -> put ^.*[a-zA-Z]+.*\n In the find section and keep replace with blank. Hit the replace all button this will delete all the rows with even a single string in it.
I like the manual edition solution, very good.
But.. have you tried to use cat and grep -v to filter out the lines and redirect to another file? Maybe better than learning regex.. (personally I always start with regex and end with editing the files myself).
In Windows you use findstr /v.
So you would do:
# in bash
cat my.log | grep -v "No records to send and/or not connected" > new.log
or
# in cmd
cat my.log | findstr /v "No records to send and/or not connected" > new.log
I ran into a similar problem editing a sitemap
This worked for me:
Copy the last word in the lines that you want to delete
Find all
Press delete to delete the entire line
Find -> Find all (this will mark the lines having the keyword)
Then go to Edit->Line->Delete line
When I run the command
git cherry origin/Server_Dev
in my git repository, I get a list of commits of the form
+ 95b117c39869a810595f1e169c64e728d2d7443d
+ e126f1b996ecf1d2a8cf744c74daa92cce338123
+ 869169a6cb0bbe8f1922838798580a1e74ec3884
+ 667819b617c88bd886dc2001f612b5c7a4d396c3
+ fd41328a84b0a127affa6fe4328c93e933de378c
+ cfe1807e5d4acc6b5e75f4463dadb3b1c957376f
This is a good thing.
I now want to execute this command from within a bash script and capture the output into an array using the following code:
commit_hashes=(`git cherry origin/Dev`)
echo ${commit_hashes[#]}
which yields the following output:
+ 95b117c39869a810595f1e169c64e728d2d7443d + e126f1b996ecf1d2a8cf744c74daa92cce338123 + 869169a6cb0bbe8f1922838798580a1e74ec3884 + 667819b617c88bd886dc2001f612b5c7a4d396c3 + fd41328a84b0a127affa6fe432
8c93e933de378c + cfe1807e5d4acc6b5e75f4463dadb3b1c957376f
This is not a good thing
My list of commits is being returned as a string which I must first break up before I can use it. After some searching I found out that if I add IFS="" to my script before the capturing of the data, my problems would be solved.
So I edited my code to read
IFS=""
commit_hashes=(`git cherry origin/Dev`)
echo ${commit_hashes[#]}
which output
+ 95b117c39869a810595f1e169c64e728d2d7443d
+ e126f1b996ecf1d2a8cf744c74daa92cce338123
+ 869169a6cb0bbe8f1922838798580a1e74ec3884
+ 667819b617c88bd886dc2001f612b5c7a4d396c3
+ fd41328a84b0a127affa6fe4328c93e933de378c
+ cfe1807e5d4acc6b5e75f4463dadb3b1c957376f
This completely ended my sense of reality.
I like to know why things are doing what they're doing, so after some more searching I found out that this is called the Internal Field Separator and it is used on Unix systems by command interpretors to figure where to break patterns up into tokens.
This I understand.
What I don't understand is
Why setting this variable to an empty string allowed it to handle my array data in a sane manner.
Why I had to set it so in the first place, instead of the interpretor realising it was dealing with array data and handling it appropriately.
What effect setting the Internal Field Separator to an empty string will have in the grand scheme of things, since by default it contains the characters for a space, a tab and a newline.
Some help in getting my head around these three points would be appreciated.
See man bash/Arrays. Everything is fine with your array. When you do
echo ${commit_hashes[#]}
echo shows all array elements on one line, as it is told to. When you say
for i in `seq 10`; do
echo ${commit_hashes[$i]}
done
you will see that just one entry per line is shown.
When you set IFS= to an empty string however, the result of git cherry isn't broken up into multiple values. The whole string including the newlines is assigned to the first array element. If you
echo ${commit_hashes[0]}
in your second case, you will see, that echo shows all output lines.