Ruby string single quotes causing trouble - ruby-on-rails

I am trying to create a cronjob using whenever gem.
every 1.day, :at => "12:00pm" do
grep_part_of_command = '"#timestamp":"'+Date.today.to_s
command "cat logstash_development.log | grep '#{grep_part_of_command}' > todays_logstash_development.log"
end
What I want to achieve:
* * * * * /bin/bash -l -c 'cat logstash_development.log | grep '"#timestamp":"2016-04-20' > todays_logstash_development.log'
But when I open my crontab, what I get is :
* * * * * /bin/bash -l -c 'cat logstash_development.log | grep '\''"#timestamp":"2016-04-20'\'' > todays_logstash_development.log'
Note the extra '\' around the grep matcher string.
Can anyone help me find my mistake.

That seems correct! Whenever uses single quotes everywhere, so that special symbols like ! are not interpreted in shell. '\'' is a way of print single quote in a single quoted string. Try the following:
echo 'grep '\''"#timestamp":"2016-04-20'\'' > '
It will output:
grep '"#timestamp":"2016-04-20' >
So, don't worry! The output text is correct.

Related

Grep for lines that starts with " followed by 10 digits ",""

e.g.
Find line "1437421130",""
but not "1437421130","92729392"
Cant figure out how to handle double quotes. ( I am an idiot when it comes to grep )
I tried
echo "1437421130","" | grep '"\d{10}",""'
echo "1437421130","" | grep '"[0-9]{10}",""'
Did this on centos. Used single quotes to avoid the double quote escaping. -E to allow the {10}

remove word using "grep -v" exact match only

How can I do an exact match using grep -v?
For example: the following command
for i in 0 0.3 a; do echo $i | grep -v "0"; done
returns a. But I want it to return 0.3 a.
Using
grep -v "0\b"
is not working
for i in 0 0.3 a; do echo $i | grep -v "^0$"; done
You need to match the start and end of the string with ^ and $
So, we say "match the beginning of a line, the char 0 and then the end of the line.
$ for i in 0 0.3 a; do echo $i | grep -v "^0$"; done
0.3
a
The safest way for single-column entries is using awk. Normally, I would use grep with the -w flag, but since you want to exactly match an integer that could be part of a float, it is a bit more tricky. The <dot>-character makes it hard to use any of
grep -vw 0
grep -v '\b0\b'
grep -v '\<0\>'
The proposed solution also will only work on perfect lines, what if you have a lost space in front or after your zero. The line will fail. So the safest would be:
single column file:
awk '($1!="0")' file
multi-word file: (adopt the variable FS to fit your needs)
awk '{for(i=1;i<=NF;++i) if($i == "0") next}1' file

How do I 'grep -c' and avoid printing files with zero '0' count

The command 'grep -c blah *' lists all the files, like below.
% grep -c jill *
file1:1
file2:0
file3:0
file4:0
file5:0
file6:1
%
What I want is:
% grep -c jill * | grep -v ':0'
file1:1
file6:1
%
Instead of piping and grep'ing the output like above, is there a flag to suppress listing files with 0 counts?
SJ
How to grep nonzero counts:
grep -rIcH 'string' . | grep -v ':0$'
-r Recurse subdirectories.
-I Ignore binary files (thanks #tongpu, warlock).
-c Show count of matches. Annoyingly, includes 0-count files.
-H Show file name, even if only one file (thanks #CraigEstey).
'string' your string goes here.
. Start from the current directory.
| grep -v ':0$' Remove 0-count files. (thanks #LaurentiuRoescu)
(I realize the OP was excluding the pipe trick, but this is what works for me.)
Just use awk. e.g. with GNU awk for ENDFILE:
awk '/jill/{c++} ENDFILE{if (c) print FILENAME":"c; c=0}' *

tcl, grep and the greater than sign

I am trying to grep for a string that contains a greater than sign in tcl.
What I tried so far:
grep -orI -- {\> somestring} dir
but that does not appear to work.
Then I tried looking for the greater than sign only:
grep -orI -- \> dir
That did not work..
You can do stuff to ensure that > is not the first character of a word in the exec cmd: for example, using otherwise needless capturing parentheses
$ tclsh
% exec cat ./afile
line1
line2 > somestring
line3
% exec grep -orI {> somestring} .
[waits for me to hit ctrl-d]
child process exited abnormally
% exec grep -orI {\(> somestring\)} .
./afile:> somestring
I found this question and answer:
how to pass command line parameter containing '<' to 'exec'
I realized that exec and > are not friends in tcl. I will find a creative way to overcome this or use another tool for this task.
eval is your friend:
set cmd "grep this someFile.txt > outputfile.txt"
eval $cmd

how to delete line which contain some specific string in shell

Plz help me to delete line which contain / or ab in linux shell
Example:
ghkl
aaaabd
fdkh/dfd
hjnh
after filter : we got:
ghkl
hinh
Thanks !
Using awk:
awk '!/\/|ab/' file
ghkl
hjnh
Using grep:
grep -v "/\|ab" file
ghkl
hjnh
-v inverts the action to not print line with this pattern
'!/\/|\?/'
' start of awk command
! a not, do do not find this
/ start of regex find whats between / and /
\ escape the next character so its not treated as a command
/ since this is escaped find this /
| or. Find this or this
\ a new escape to prevent ? used as code
? since escaped find ?
/ end of regex
' end of awk command

Resources