Grep string inside double quotes
Try doing this :
$ cat aaaa
foo"bar"base
$ grep -oP '"\K[^"\047]+(?=["\047])' aaaa
bar
I use look around advanced regex techniques.
If you want the quotes too :
$ grep -Eo '["\047].*["\047]'
"bar"
Note :
\047
is the octal ascii representation of the single quote
grep a string inside double quotes
You can write your loop a little more efficiently and use sed
instead of multiple grep
s to get what you want:
for f in test.txt; do
echo "Processing $f"
while read line; do
grep 'name="'$line'"' new1.xml
done < "$f" | sed -E 's/.+path="([^"]+)".+/\1/'
done
For your example, the above script gives this output:
Processing test.txt
abc/test/test
If you are just processing one file, you don't need the outer loop:
while read line; do
grep 'name="'$line'"' new1.xml
done < "test.txt" | sed -E 's/.+path="([^"]+)".+/\1/'
Escape double quote in grep
The problem is that you aren't correctly escaping the input string, try:
echo "\"member\":\"time\"" | grep -e "member\""
Alternatively, you can use unescaped double quotes within single quotes:
echo '"member":"time"' | grep -e 'member"'
It's a matter of preference which you find clearer, although the second approach prevents you from nesting your command within another set of single quotes (e.g. ssh 'cmd'
).
grep between some characters (quotes, etc) of after (eg. hashtag) any content (text, numbers, emojis)
Thanks to @Aserre's pointings, I could come up with an answer.
In order for the "get every text when it appear AFTER a charater" and "get every text when it appear BETWEEN quotes" (grep) to work with any character, we have to replace [[:alpha:]]
in the block to ...
So, it is:
echo '#first = "✅ Yes"' | grep -o '"...\+"' | tr -d '"'
(get anything which is between double quotes)
and:
echo "Text and #hashtag" | grep -o '#...\+' | tr -d '"'
(get anything which is after a hashtag)
Update:
If you want to support things with only 1 character (such as numbers ranging from 0 to 9), replace ...
to .
(single dot)
It works, as in the question, for: emojis, letters, numbers and other special characters.
How do I use GREP to match a string with quotation marks inside it?
With traditional regex, the double quotes are escaped by the single quotes. You only need to escape the quantifier +
(one or more) and the literal dot in .mp4
grep -o '="http[^"]\+\.mp4"'
With PCRE, available in GNU Grep, you can actually match (without printing) the leading/trailing equals/doublequotes with Lookarounds:
grep -Po '(?<==")http[^"]+\.mp4(?=")'
(?<= ... )
- lookbehind(?= ... )
- lookahead
returns:
https://www.something.com/file1.mp4
https://www.something.com/file2.mp4
https://www.something.com/file3.mp4
https://www.something.com/file4.mp4
How to search for a string with one double quote with git grep in Windows cmd
In Git CMD or Windows cmd console, you can use ^
to escape the "
inside the terminal:
git grep ""^""Agent" > 1.txt
In Git Bash, you can simply use
git grep '"Agent' > 1.txt
See this text I made on my data with "Regex
pattern:
How to match double quotes using grep
Use single quotes to consider literal meaning of every character.
grep 'and "Ben" is' trial.txt
How to find a double quoted string using grep and regex?
Your expression doesn't make sense to me. It seems like you're looking for:
grep -R '5\.1' .
Or if you want to include the "
characters:
grep -R '"5\.1"' .
But I don't see anything related to those in your original post, so it's hard to say.
grep with double quotation in bash script
The simplest adaptation of the code you're using is:
for ip in $(cat /tmp/iplist)
do
grep "$ip" /tmp/whitelist
done
Once you've got the value in the variable, its double quotes aren't damaged by further double-quoted variable expansion. If the /tmp/iplist
file doesn't contain the double quotes but they're critical, then you can use:
grep "\"$ip\"" /tmp/whitelist
or you could use this:
grep \""$ip"\" /tmp/whitelist
(and there are two asymmetric permutations available too). It's a good idea to make sure you do know why that works. There are some ways to use single quotes if you want to, but the "$ip"
part must be outside of the single quotes.
All your examples start the pattern argument to grep
with a single quote. Single quotes suppress all expansions until the next single quote. So, for example, grep '"$ip"' /tmp/whitelist
is looking for 5 characters — "
, $
, i
, p
, "
— in the file. In none them is the variable ip
ever expanded.
There will be problems if any IP address ever gets a space in it. Be cautious about using for ip in $(cat /tmp/iplist)
. Very often you'd do better with:
while read -r ip
do
grep "$ip" /tmp/whitelist
done < /tmp/iplist
Another way to do this is with grep -F
or fgrep
:
grep -F -f /tmp/iplist /tmp/whitelist
This doesn't insist on double quotes around the IP addresses, but makes a single pass over the /tmp/whitelist
file (and a single pass over the /tmp/iplist
file too), which is about as efficient as it gets. This will produce lines in a slightly different order from before, which probably won't matter, but you should be aware of it.
If you must have the double quotes (to avoid selecting 11.2.3.44
when searching for 11.2.3.4
, then:
grep -F -f <(sed 's/^/"/; s/$/"/' /tmp/iplist) /tmp/whitelist
This uses process substitution to pass an edited version of the /tmp/iplist
file to the grep
command. If you don't have process substitution in your shell, you can probably use:
sed 's/^/"/; s/$/"/' /tmp/iplist | grep -F -f - /tmp/whitelist
which makes grep
read the list of patterns to match from standard input instead of a named file. If perchance -f -
doesn't work (e.g. because you're working on a Mac using macOS, or probably a BSD machine), then -f /dev/stdin
probably will, or -f /dev/fd/0
.
You could also generate the /tmp/iplist
file with the double quotes in place. You could generate the /tmp/whitelist
file without the double quotes and then use grep -x
to specify an exact match.
In case you haven't already gathered, there are quite a lot of different ways to do this.
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.
Related Topics
200,000 Images in Single Folder in Linux, Performance Issue or Not
Differencebetween Module_Init and Init_Module in a Linux Kernel Module
How to Echo a Sum of a Variable and a Number
Running Shell Script Using .Env File
For Loop for Files in Multiple Folders - Bash Shell
Making Gcc Prefer Static Libs to Shared Objects When Linking
How to Know Which Device Is Connected in Which /Dev/Ttyusb Port
Linux Assembly: How to Call Syscall
Any Way to Specify the Location of Profile Data
How to Redirect Nohup Output to a Specified File
Linux Command to Replace String in Large File with Another String
Linker Cannot Find Symbols, But Libraries Are Read and Symbols Exist
Elf Dynamic Loader Symbol Lookup Ordering
Poll(2) Doesn't Empty the Event Queue
Linux/Perl: Additional Output Buffers Other Than Stdout and Stderr
How to Detect Usb Device Disconnect Under Linux/Qt/C++
How to Pass an Environment Variable to a Netbeans Makefile on Ubuntu
Convert Animated Gif to Video on Linux Server While Preserving Frame Rate