2

I have the following text

availableMemoryMb: 1024 buildId: b12dfv231-3422-4dsvec-adfdfa-1dvf13vd8 entryPoint: get_request_data httpsTrigger: securityLevel: SECURE_OPTIONAL url: https://us-central1-vvsfdv ds.cloudfunctions.net/evses ingressSettings: ALLOW_ALL labels: deployment-tool: cli-gcloud maxInstances: 1 name: projects/sasvbsd/locations/us-central1/functions/adfva runtime: python37 serviceAccountEmail: abcd-def@appspot.gserviceaccount.com sourceUploadUrl: https://storage.googleapis.com/gcf-upload-us-central1-ed32d2-7232322-44h73-96f0-6610891aa7ca/8f5950a6-0efd-4cca-9297-b5104156208b.zip?GoogleAccessId=service-12332@gcf-admin-robot.iam.gserviceaccount.com&Expires=1619892863&Signature=aH1%2BW741GRtVNM1rrwpz8jfxzhNCnQ%2BP5of%2Bq0ghMrcsmR6lM6PgJnHFVTZ5FsWC484rUhFIhBj5IYNEP0Egw4VA0Ui9o3hQSL9NdqBUMtmLM%2BqKKagHVHtUm1Rfr6U4xRxUm4z0SiHNSMB5aZEfwbdmCj6r%2FEx5HuKp5c9HNyJ8LYXynBrjztlZr9GkmHkyHMPM9CjW0c33BMeEWHEGqxUAj%2FyPksPn7y9WPbQFCiwUiQbd3ayuW%2FMVC53IVwjxkojrzLATGVvu12%2Brc9fQs7Zz%2FEc1ZthUVwBAdBYVj8Fn%2Fde5S7OJwdRUm938N%2B5gCX4x%2B7s25Bq6Lgk%2FLWesLw%3D%3D status: ACTIVE timeout: 60s updateTime: '2021-05-01T17:45:13.094Z' versionId: '7'

from which i need to extract the versionId (in this case: 7) which is last (not always) , here is my regular expression sed 's/\(.*\)versionId:\s+\'\([0-9]+\)\'/\2/' and not sure why its not working, its coming back with entire content of my input

1 Answers1

3

With your shown samples, please try following in sed. You need not to create 2 back references, just one will enough for this task.

sed -E 's/.*versionId:[[:spaces:]]+'"'"'([0-9]+)'"'"'/\1/'  Input_file

OR(as per hilipati's comment above could be written as):

sed -E "s/.*versionId:[[:space:]]+'([0-9]+)'/\1/" Input_file

Explanation: In sed using -E option which enables ERE(extended regular expressions) in program. Then using s option of it to perform substitution where matching everything till versionId: spaces ' and keeping following digits into 1st capturing group, while substituting, substitute whole line with only digits, which will give only digits as output as per requirement.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Thanks @RavinderSingh13, that didnt helped, i tried same previously. wish i can attach the screenshot here – loveprogramming May 02 '21 at 05:01
  • @loveprogramming, this has worked for me fine, I had tested it, try `sed -E 's/.*versionId:[[:space:]]++'"'"'([0-9]+)'"'"'/\1/' Input_file` once? and let me know then. – RavinderSingh13 May 02 '21 at 05:02
  • 1
    I find this can be further simplified to `sed -E "s/.*versionId:\s+'([0-9]+)'/\1/g" input-file` – hilipati May 02 '21 at 05:06
  • 1
    @hilipati, sure, thank you. I have added this solution too in my answer now, cheers. – RavinderSingh13 May 02 '21 at 05:10
  • Nope that didnt helped , i am using mac terminal bash 3.2, not sure if that have any thing to do – loveprogramming May 02 '21 at 05:12
  • 1
    Thanks all , i figured it has something to do with my mac bash , tried same on a server and it worked – loveprogramming May 02 '21 at 05:13
  • @loveprogramming, sure, thanks for informing. You could try `awk 'match($0,/versionId:[[:space:]]+\047[0-9]+/){val=substr($0,RSTART,RLENGTH);gsub(/[^0-9]+/,"",val);print val}' Input_file` too for your mac once and let me know if this helps you? This should work irrespective of o.s – RavinderSingh13 May 02 '21 at 05:14
  • 1
    @loveprogramming, Apparently mac sed is indeed different from linux sed. Here is an answer for a related problem which might help in the future: https://stackoverflow.com/a/41029130/13099243 (installing gnu-sed on mac) – hilipati May 02 '21 at 05:21