1

I have very strange interest to pattern match a line for a string and extract a value using grep. Below is the input and I want to extract the date alone from the string.

Input Host-GOOGLE-production.2015-08-01-21.migrant.deploy:{R:[{A:"0b87654nuy",RC:"JAVA".....[and the line continues]

For the above input, I wanted to write a regex that matches the date and string that comes after {A:" and before ",RC:. I know I can do this through sed and awk but I wanted to perform this task only through grep.

As a first step, to extract only the data, I tried the below command but it dint work. Someone know how to extract both these strings to extract the values. please share your thoughts. It would be nice if I get an answers/suggestion that extract both values 2015-08-01 & 0b87654nuy in one single command using grep

$grep -o --perl-regexp "(Host-GOOGLE-production.([0-9]+?-[0-9]+?-[0-9]+)?-.*)"

Desired O/P for the above command: 2015-08-01

user3624000
  • 291
  • 1
  • 5
  • 17

2 Answers2

2

I wanted to write a regex that matches the date and string that comes after {A:" and before ",RC:

You can use this grep:

grep -oP '(?<=A:").*?(?=",RC:)' file
0b87654nuy
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Need a small correction in my command. I have narrowed my above command to get the output `2015-08-01-21,0b87654nuy`. But I need only the date and value ie) `2015-08-01,0b87654nuy`. In order to get rid of number '21' from the o/p I got, I wrote the command `sed -e '\([0-9]+-[0-9]+-[0-9]+\)-[0-9]+\,\([a-zA-Z0-9]+\)$/\2/'`but din succeed. Can someone guide what I am missing there? – user3624000 Aug 19 '15 at 10:36
  • Hmm that doesn't seem to be related with my `grep` command but since you asked you can use: `sed -r 's/([0-9]+-[0-9]+-[0-9]+)-[0-9]+(,[a-zA-Z0-9]+)$/\1\2/'` – anubhava Aug 19 '15 at 10:42
0

It would be nice if I get an answers/suggestion that extract both values 2015-08-01 & 0b87654nuy in one single command using grep

Use \K and alternation operator to get both outputs.

grep -oP '\bHost-GOOGLE-production\.\K[0-9]+-[0-9]+-[0-9]+(?=-)|A:"\K.[^"]*(?=",RC:)'

Example:

$ echo 'Host-GOOGLE-production.2015-08-01-21.migrant.deploy:{R:[{A:"0b87654nuy",RC:"JAVA".....[and the line continues]' | grep -oP '\bHost-GOOGLE-production\.\K[0-9]+-[0-9]+-[0-9]+(?=-)|A:"\K.[^"]*(?=",RC:)'
2015-08-01
0b87654nuy
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thanks Avinash. Just to know, what is the purpose of `\K`. I could not find it being in the options of `grep` – user3624000 Aug 18 '15 at 10:48
  • it belongs to PCRE http://stackoverflow.com/questions/13542950/support-of-k-in-regex – Avinash Raj Aug 18 '15 at 10:50
  • The solution `grep -oP '\bHost-GOOGLE-production\.\K[0-9]+-[0-9]+-[0-9]+(?=-)|A:"\K.[^"]*(?=",RC:)'` is not working.. no results neither no o/P nor.. – user3624000 Aug 18 '15 at 11:09