1

I use the below command to grep and get the substring that lies between two strings.

echo "This is an example" | grep -o -P '(?<=This).*(?=example)'

which would give is an

Now I got a huge amount of raw string that has the below format

"],"id":"1785695Jkc","

I tried to use the above mentioned command for this String too. But it did not work.

grep -o -P '(?<="],"id":").*(?=",")'
codeforester
  • 39,467
  • 16
  • 112
  • 140
Jeeppp
  • 1,553
  • 3
  • 17
  • 39
  • Your tags imply that you're looking for help with [tag:bash] and [tag:awk], but you haven't included bash or awk code in your question. Is it really `grep` you want help with, or would you like to show us what you've done so far in bash or awk so we can help you with those solutions? Also, what exactly is the output you're looking for, given the sample input you've provided? – ghoti Feb 16 '18 at 04:38

2 Answers2

2

Note that the shell removes the outer quotes from strings:

$ echo "This is an example"
This is an example
$ echo "],"id":"1785695Jkc","
],id:1785695Jkc,

If you want to keep the double-quotes, they need to be quoted. Here, we put the string in single-quotes and this preserves the double-quotes:

$ echo '"],"id":"1785695Jkc","'
"],"id":"1785695Jkc","

Now, your grep command will work:

$ echo '"],"id":"1785695Jkc","'  | grep -o -P '(?<="],"id":").*(?=",")'
1785695Jkc

Documentation

Quote removal is documented in man bash:

Quote Removal
After the preceding expansions, all unquoted occurrences of the characters \, ', and " that did not result from one of the above expansions are removed.

John1024
  • 109,961
  • 14
  • 137
  • 171
2

You haven't stated what output you're looking for. But awk probably lets you pull out content from a comma-separated set of values most easily:

$ s='"],"id":"1785695Jkc","'
$ awk -F, '{print $2}' <<<"$s"
"id":"1785695Jkc"
$ awk -F, '{split($2,a,":"); print a[2]}' <<<"$s"
"1785695Jkc"

Or if you prefer,

$ awk -F: -v f='"id"' '{a[$1]=$2} END {print a[f]}' RS=, - <<<"$s"
"1785695Jkc"

This second notation reads in comma separated value pairs that use a colon as field separator, so you could also do this:

$ awk -F: -v f='id' '{a[$1]=$2} END {print a[f]}' RS=, - <<<"one:red,id:hello,foo:bar"
hello
ghoti
  • 45,319
  • 8
  • 65
  • 104