0

I am developing a shell script that will replace logglyKey from environment.ts file.

// environment.ts
{
    production: true,
    logglyKey: 'asdfasfd-asdfsd-asdfs-asdfsdf-asdfasfd'
}

I want to replace logglyKey on build process from environment variables from bitbucket pipelines. How can I achieve this?

I tried this, but it doesn't extract the logglyKey correctly.

// addLogglyKey.sh
#!/bin/bash

export LOGGLY_KEY=$(grep -oP "'logglyKey'\s*:\s*'([A-Za-z0-9-])'" ./src/environments/environment.ts)
echo "Previous logglyKey was : ${LOGGLY_KEY}"

But this prints empty logglyKey.

Previous logglyKey was : 
Piyush Patel
  • 1,646
  • 1
  • 14
  • 26
  • 1
    Key `logglyKey` is not quoted in your example but is quoted in the pattern `'logglyKey'`. – LMC May 18 '18 at 20:37

2 Answers2

1

Fixing the regular expression will return a result with grep but it's probably not what you expect

grep -oP "logglyKey\s*:\s*'([A-Za-z0-9-]+)'" test.txt

Result:

logglyKey: 'asdfasfd-asdfsd-asdfs-asdfsdf-asdfasfd'

Trying to set a non-capturing group (supported by Perl regexp) doesn't work either

grep -oP "^(?:\s*logglyKey\s*:\s*')([A-Za-z0-9-]+)'\s*$" test.txt
logglyKey: 'asdfasfd-asdfsd-asdfs-asdfsdf-asdfasfd'

Can be done using sed

sed -nre "s/^\s*logglyKey\s*:\s*'([A-Za-z0-9-]+)'$/\1/p" test.txt
asdfasfd-asdfsd-asdfs-asdfsdf-asdfasfd
LMC
  • 10,453
  • 2
  • 27
  • 52
  • I m getting `sed: -e expression #1, char 84: unterminated `s' command` for `sed -i "s/${LOGGLY_KEY}/${VERSION}/" ./environments/environment.*` Here, I want to edit two files in environments directory, environment.ts and environment.prod.ts files. Can you suggest me what's wrong? – Piyush Patel May 18 '18 at 21:01
  • Print the sed pattern to check what's wrong `echo "s/${LOGGLY_KEY}/${VERSION}/"`. – LMC May 18 '18 at 21:04
  • No I couldn't find anything informative. In fact the command works on single file like environment.ts file, but in `environments` directory, I have two directories `environment.ts` and `environment.prod.ts`. Probably that's causing it to crash. I have included the code here. `#!/bin/bash export LOGGLY_KEY=$(grep -oP "logglyKey: '([\\w-]+)'" environments/environment.*) echo "Previous logglyKey was : ${LOGGLY_KEY}" export VERSION=$(echo "logglyKey: '${NEW_LOGGLY}'") echo "New Version number: ${VERSION}" sed -i "s/${LOGGLY_KEY}/${VERSION}/" environments/environment.*` – Piyush Patel May 22 '18 at 00:19
0

Keep it simple, just use awk:

$ awk -v new='fluffy-bunny' 'BEGIN{FS=OFS="\047"} /logglyKey:/{$2=new} 1' file
// environment.ts
{
    production: true,
    logglyKey: 'fluffy-bunny'
}

grep is just for finding a regexp in a file and printing the matching line (g/re/p - get it?) not for modifying files, and sed doesn't understand string literals so you'll quickly be in escaping/quoting hell if you try to use that for any data that can contain other than alpha-numeric chars, see Is it possible to escape regex metacharacters reliably with sed.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185