how to escape single quotes in grep
Just wrap the string within double quotes:
grep "'true' format" your_file
Test
$ cat a
this is 'true' format
and this is true formatting
test
$ grep "'true' format" a
this is 'true' format
How to escape single quotes in Bash/Grep?
grep -i "something ~\* '[[:alnum:]]*'" /var/log/syslog
works for me.
- escape the first
*
to match a literal*
instead of making it the zero-or-more-matches character:~*
would match zero or more occurrences of~
while~\*
matches the expression~*
aftersomething
- use double brackets around
:alnum:
(see example here) - use a
*
after[[:alnum::]]
to match not only one character between your single quotes but several of them - the single quotes don't have to be escaped at all because they are contained in an expression that is limited by double quotes.
how to grep everything between single quotes?
Try something like this: sed -n "s#version:\s*'\(.*\)'#\1#p" myfile.txt
. This avoids the redundant cat
and grep
by finding the "version" line and extracting the contents between the single quotes.
Explanation:
the -n
flag tells sed not to print lines automatically. We then use the p
command at the end of our sed pattern to explicitly print when we've found the version line.
Search for pattern: version:\s*'\(.*\)'
version:\s*
Match "version:" followed by any amount of whitespace'\(.*\)'
Match a single'
, then capture everything until the next'
Replace with: \1
; This is the first (and only) capture group above, containing contents between single quotes.
How to do a grep regex search for single-quotes?
You are using a PCRE. So, you need the -P
flag. So, use this:
grep -rnwi some/path -P "ABC\s*=\s*[\'\"][^\'\"]+[\'\"]"
We don't need a \\
for single quotes inside the character classes. So, your regex can also be written as:
"ABC\s*=\s*['\"][^'\"]+['\"]"
Input file:
ABC="123"
ABC='123'
Run grep
with your PCRE:
grep -P "ABC\s*=\s*['\"][^'\"]+['\"]" input.txt
Output:
ABC="123"
ABC='123'
extract path between single quotes using grep
Since the question asks for a solution in grep
, a single GNU grep
command to extract the specified path could be:
grep -Po "^Saving to: .\\K[^']*"
provided the Perl Regular Expressions are implemented in the grep
(not all grep
s implement those).
Of course, it can be used in a pipe also:
wget_command | grep -Po "^Saving to: .\\K[^']*" | tee log.txt
Note that I used a single quote ('
) character to anchor the end of path in the pattern match expression, but in the question, Unicode Character Left Single Quotation Mark (U+2018) (‘
) and Unicode Character Right Single Quotation Mark (U+2019) (’
) are used in the sample input. If this is really intended then just replace the [^']
with the [^’]
in the pattern match expression above.
bash string expansion for grep -E expression in single quotes
Why are you adding the single quote marks? Just remove this line:
EXCLUDE_PATTERN="'""$EXCLUDE_PATTERN""'"
I'm getting the following without that line:
Excluding files that match the string alice|bar|bob|foo
Keeping alice.tmp
Keeping bob.tmp
Deleting crump.tmp
Deleting dammitall.tmp
Keeping foo.tmp
How do I escape slashes and double and single quotes in sed?
The s///
command in sed
allows you to use other characters instead of /
as the delimiter, as in
sed 's#"http://www\.fubar\.com"#URL_FUBAR#g'
or
sed 's,"http://www\.fubar\.com",URL_FUBAR,g'
The double quotes are not a problem. For matching single quotes, switch the two types of quotes around. Note that a single quoted string may not contain single quotes (not even escaped ones).
The dots need to be escaped if sed
is to interpret them as literal dots and not as the regular expression pattern .
which matches any one character.
bash: matching single and double quotes together by grep
I am probably mis-interpreting the question, but it seems like you are asking how to deal with properly escaping the quotes. Just do:
grep '\(["'"']\).*\1.*\1"
Search for single quoted grep strings?
The main problem here is that you are not quoting the argument being passed to grep. The only thing that needs to be escaped is \$ (if double quoted) and []. If you want the exact string (not using regex), just use fgrep (grep -F) which does exact string matching:
grep -F "\$server['fish_stick']"
Works on my system:
$ foo="\$server['fish_stick']"
$ echo "$foo" | grep -F "\$server['fish_stick']"
$server['fish_stick']
Using regex:
$ echo "$foo" | grep "\$server\['fish_stick'\]"
$server['fish_stick']
Using regex and handling nested single quotes:
$ echo "$foo" | grep '\$server\['\''fish_stick'\''\]'
$server['fish_stick']
Inside of single quotes, nested single quotes can not be not be escaped. You have to close the quotes, and then reopen it to "escape" the single quotes.
http://mywiki.wooledge.org/Quotes
Related Topics
How to Fix Java.Lang.Module.Findexception: Module Java.Se.Ee Not Found
How to Remove a Close_Wait Socket Connection
How to Store a Command in a Variable in a Shell Script
How to Prevent a Background Process from Being Stopped After Closing Ssh Client in Linux
What Happens to an Open File Handle on Linux If the Pointed File Gets Moved or Deleted
Pseudo-Terminal Will Not Be Allocated Because Stdin Is Not a Terminal
Static Link of Shared Library Function in Gcc
How to Set the Working Directory of the Parent Process
Linking Against an Old Version of Libc to Provide Greater Application Coverage
How to Get Terminal'S Character Encoding
Separately Redirecting and Recombining Stderr/Stdout Without Losing Ordering
The Bash Command :(){ :|:& };: Will Spawn Processes to Kernel Death. Can You Explain the Syntax
Is Gettimeofday() Guaranteed to Be of Microsecond Resolution
How to Find the Original User Through Multiple Sudo and Su Commands
How to Get Full Path of a File