How to find this string in multiple files with grep? - grep

I'm trying to use grep to find the string "init()" in all the files that end in .js in a directory.
I tried this
grep "init()" *.js
but it didn't work. Why not? What's the correct way to do this?

Try escaping the parentheses: grep "init\(\)" *.js
It is actually a regular expression, where ( and ) are special characters.

Related

Carrierwave: Use spaces instead of underscore

We have an uploader for PDF's. When a file name has spaces in it, they are automatically being converted to use underscores:
some file test -> some_file_test
I'd like to keep the spaces. Can someone tell me how?
I tried:
def filename
original_filename
end
You can override sanitize regexp by adding whitespace:
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+\ ]/
As you see this regexp used in sanitize method that replace forbidden symbols to underscore.
From CarrierWave documentation:
Filenames and unicode chars
Another security issue you should care for is the file names (see Ruby On Rails Security Guide). By default, CarrierWave provides only English letters, arabic numerals and some symbols as white-listed characters in the file name. If you want to support local scripts (Cyrillic letters, letters with diacritics and so on), you have to override sanitize_regexp method. It should return regular expression which would match all non-allowed symbols.
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party plugins or client-side software.
Try:
original_filename.gsub("_", " ")
UPDATE (possible workaround):
Replace underscores with a character or a string (e.g. "xyxyxyxyxyxyxyxyz") you are not expecting in filenames before passing them to carrierwave i.e.
filename.gsub("_", "your_special_character/s")
Replace underscores with spaces and special character with underscores later:
original_filename.gsub("_", " ")
original_filename.gsub("your_special_character/s", "_")

Relative file paths in Lua?

I'm having some trouble using relative file paths in Lua, I'm not sure what the syntax is supposed to be.
If I have something like:
Parent Directory: File1.lua
|
|-----> Folder1: script.lua
If I wanted to use script.lua in File.Lua how would I format the dofiles(), require ' ', and filename strings?
dofile and filename strings can use "Folder1/script.lua".
For require, use require"Folder1.script".

Grep-ing from multiple files

I have a lot of log files with format foo.log.[1-100].gz, and another one detail-20161205-[00-23]. Need to find some string from multiple files.
I'm trying to do the following:
zfgrep String foo.log.[45-64].gz,
but I'm always getting wrong output, not from mentioned files.
Thus, I want to understand how to grep from .gz files and from not .gz (from the second format). Can I use commands other than grep as well?
I believe you should be able to do something like grep String foo.log.{1..100}.gz and grep String detail-20161205-{00..23}. If not all the files exist in that range, you can add the -s option so you don't see all the errors.
grep -s String foo.log.{1..100}.gz
What detail-20161205-[00-23] does for example is expand to 0, 0-2, 3, leading to the wrong files being searched.

Ruby- how to get specific keyword from files/folder

I need to go through in each and every folders/files in php to find some specific keyword in this case translation format e.g $this-> and $translator->.
I need to get those result and put it on to new files.
Here is what I have tried before using ruby.
this = File.readlines("folder_path.php")
#If I need to get any translation that contain $this-> should I use grep? I tried using grep before but not giving result that I need.
that = File.open("new_file.txt", "w")
that << this
that.close
Hope that I didn't make any confusion. Thanks.
Just use grep:
grep '$this->' -R *.php -n > result.txt

Ruby: Using a regular expression to find and open a file based on its filename?

I am trying to test the contents of a file that is generated from code. The problem is that the full name of the file is based on a timestamp abc123_#{d.strftime('%Y%m%d%I%M%S')}.log
How could I use File to find this file and read it? I tried doing File.exists?() with a regular expression as the parameter but that didn't work.
I found this in another question on stackoverflow:
File.basename(file_path).match(/_.*(css|scss|sass)/)
How would I be able to use that in my case where the file is located in mypublic folder?
ANSWER
So the two answers below both work and I used a combination of them.
Dir['public/*.log'].select { |f| f =~ /purge_cc_website/}
The * acts as a wildcard that is sort of a regular expression in itself. After that you filter the array using an actual regex.
Dir[] takes a file glob so, if your pattern isn't too complicated, you can just do:
Dir['public/abc123_*.log']
More glob info here.
File is for reading one file. You need to use Dir to find files by name.
files = Dir['*'].select {|x| x =~ /_.*(css|scss|sass)/ }
If you just want the last file in the case of dups:
files = Dir['*'].select {|x| x =~ /_.*(css|scss|sass)/ }.sort.last

Resources