How to get match[1] with grep? - grep

Here's my code so far...
cat config.json | grep -Po '"server"\s*:\s*"([^"]*)"'
But I just want the part within (parentheses). I can't use a look-behind because it's variable-length. What are my options?
Sample input 1:
{"debug":false,"server":"dev-dutch","env":"dev"}
Sample input 2:
{
"debug": false,
"server": "dev-dutch",
"env": "dev"
}
Desired output for both:
dev-dutch
I know there are probably safer/better ways to parse JSON, but I want to do this in shell, and it should run on both Ubuntu and FreeBSD without installing any external programs, so I'm OK with a grep hack.

With GNU grep:
grep -Po '"server": *"\K[^"]+' file

Related

using grep command to get spectfic word [LINUX]

I have a test.txt file with links for example:
google.com?test=
google.com?hello=
and this code
xargs -0 -n1 -a FUZZvul.txt -d '\n' -P 20 -I % curl -ks1L '%/?=DarkLotus' | grep -a 'DarkLotus'
When I type a specific word, such as DarkLotus, in the terminal, it checks the links in the file and it brings me the word which is reflected in the links i provided in the test file
There is no problem here, the problem is that I have many links, and when the result appears in the terminal, I do not know which site reflected the DarkLotus word.
How can i do it?
Try -n option. It shows the line number of file with the matched line.
Best Regards,
Haridas.
I'm not sure what you are up to there, but can you invert it? grep by default prints matching lines. The problem here is you are piping the input from the stdout of the previous commands into grep, and that can lack context at grep. Since you have a file to work with:
$ grep 'DarkLotus' FUZZvul.txt
If your intention is to also follow the link then it might be easier to write a bash script:
#!/bin/bash
for line in `grep 'DarkLotus FUZZvul.txt`
do
link=# extract link from line
echo ${link}
curl -ks1L ${link}
done
Then you could make your script accept user input:
#/bin/bash
word="${0}"
for line in `grep ${word} FUZZvul.txt`
...
and then
$ my_link_getter "DarkLotus"
https://google?somearg=DarkLotus
...
And then you could make the txt file a parameter.
etc.

Removing only captured group using grep

I have a list of urls, all of them look like http://example.com/page?id=1234
How can I use grep (or any tool) to remove everything that is after =
Like, the above should like this http://example.com/page?id= after running the command.
With GNU sed and its s command:
sed 's/=[^=]*$/=/' file
or GNU grep and Perl-compatible regular expressions:
grep -Po '.*=(?=.*)' file
Output:
http://example.com/page?id=
See: The Stack Overflow Regular Expressions FAQ

Find parameters of a method with grep

I need some help with a grep command (in the Bash).
In my source files, I want to list all unique parameters of a function. Background: I want to search through all files, to see, which permissions ([perm("abc")] are used.
Example.txt:
if (x) perm("this"); else perm("that");
perm("what");
I'd like to have my grep output:
this
that
what
If I do my grep with this search expression
perm\(\"(.*?)\"\)
I'll get perm("this), perm("that"), etc. but I'd like to have just the permissions: this and that and what.
How can I do that?
Use a look-behind:
$ grep -Po '(?<=perm\(")[^"]*' file
this
that
what
This looks for all the text occurring after perm(" and until another " is found.
Note -P is used to allow this behaviour (it is a Perl regex) and -o to just print the matched item, instead of the whole line.
Here is a gnu awk version (due to multiple characters in RS)
awk -v RS='perm\\("' -F\" 'NR>1 {print $1}' file
this
that
what

Bash: grep pattern to parse command output

I'm trying to parse the output of a command line tool. It outputs XML directly to STDOU and I want to parse it.
The tool outputs a full XML document like the following:
My goal is to parse that output and only the the string between the <date> tag, but since the document might contain another <date> tags, it must check only the the <date> that follows <key>SULastCheckTime</key>. (And that is a messy situation with new line/spaces there).
Currently I'm solving this situation with the following command:
tool... | grep -A1 '<key>SULastCheckTime</key>' | grep 'string.$' | sed -e 's,.*<date>\([^<]*\)</date>.*,\1,g'
It works fine but it's very messy as you can see and I can't write anything better? Can you help me making it better?
Thank you!
PS: Since I'm doing this in OSX, I don't have the new GNU grepoptions. Btw, by bash version is 3.2.48(1). And... I can't afford to install other tools to parse XML in a better way.
Maybe something like this?
$ cat foo.input
foo
foo
<key>some key</key>
<date>some date</date>
bar
bar
<key>SULastCheckTime</key>
<date>2013-08-10T00:27:40Z</date>
quux
quux
 
$ awk '/<key>SULastCheckTime<\/key>/ { toggle=1 } toggle && /<date>.*<\/date>/ { gsub(/<[^>]*>/, "", $1); print; exit }' foo.input
2013-08-10T00:27:40Z

Simple Grep Issue

I am trying to parse items out of a file I have. I cant figure out how to do this with grep
here is the syntax
<FQDN>Compname.dom.domain.com</FQDN>
<FQDN>Compname1.dom.domain.com</FQDN>
<FQDN>Compname2.dom.domain.com</FQDN>
I want to spit out just the bits between the > and the <
can anyone assist?
Thanks
grep can do some text extraction. however not sure if this is what you want:
grep -Po "(?<=>)[^<]*"
test
kent$ echo "<FQDN>Compname.dom.domain.com</FQDN>
dquote>
dquote> <FQDN>Compname1.dom.domain.com</FQDN>
dquote>
dquote> <FQDN>Compname2.dom.domain.com</FQDN>"|grep -Po "(?<=>)[^<]*"
Compname.dom.domain.com
Compname1.dom.domain.com
Compname2.dom.domain.com
Grep isn't what you are looking for.
Try sed with a regular expression : http://unixhelp.ed.ac.uk/CGI/man-cgi?sed
You can do it like you want with grep :
grep -oP '<FQDN>\K[^<]+' FILE
Output:
Compname.dom.domain.com
Compname1.dom.domain.com
Compname2.dom.domain.com
As others have said, grep is not the ideal tool for this. However:
$ echo '<FQDN>Compname.dom.domain.com</FQDN>' | egrep -io '[a-z]+\.[^<]+'
Compname.dom.domain.com
Remember that grep's purpose is to MATCH things. The -o option shows you what it matched. In order to make regex conditions that are not part of the expression that is returned, you'd need to use lookahead or lookbehind, which most command-line grep does not support because it's part of PCRE rather than ERE.
$ echo '<FQDN>Compname.dom.domain.com</FQDN>' | grep -Po '(?<=>)[^<]+'
Compname.dom.domain.com
The -P option will work in most Linux environments, but not in *BSD or OSX or Solaris, etc.

Resources