Skip to content
Advertisement

grep command in linux using ” in regex

I have the following linux cmd:

grep -o file.txt ""uri":".{1,}""

The text i have is the following:

"some characters here","uri":"some_URI*Here.^%$#!", "again a set of irrelevant characters"

Of course the output i want to have is:

"uri":"some_URI*Here.^%$#!"

Why dont i have the correct output? Because of the ” required by the grep which mix with ” in my text? How to fix it?

Advertisement

Answer

You could use the following regex:

grep -oE '"uri":".[^"]+"' inputFile

Original poster provided a regex that is almost correct but have some flaws, below is his/her version and a corrected one:

grep -o  inputFile ""uri":".{1,}""   # wrong
grep -oE '"uri":"[^"]{1,}"' inputFile   # correct

The problems with the first use of grep are:

  • inputFile should come after the regex, not before
  • Needs -E flag for {1,} to work
  • Better use single quotes outside so that double quotes need no be escaped
  • Need to use [^"] character class instead of .
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement