0

I have a bash file that has below line along with other lines.

var BUILD_VERSION       = '2014.17.10_23';

I just want to extract 2014.17.10_23 and this value may change so something like grep for 2014* . However when I do that I get the whole line returned instead of the value 2014.17.10_23.

What would be the best way to achieve this?

Thanks

jaypal singh
  • 74,723
  • 23
  • 102
  • 147
user1432622
  • 129
  • 2
  • 7
  • grep won't do this, you need awk or sed – James King May 09 '14 at 04:08
  • 1
    Your regex effectively looks for `201`; you want something like `grep -oP "^var[ ]*BUILD_VERSION[ ]*=[ ]*'\K[^']*"` – tripleee May 09 '14 at 04:15
  • @tripleee You should post that as an answer. It's a great solution and will be a good reference for others. You'll have my up-vote for it. `:)` – jaypal singh May 09 '14 at 04:50
  • @Jaypal You will find hundreds, if not thousands, of duplicates for this question. Please nominate to close as a duplicate instead of posting repetitive trivial answers. If you find a better dupicate, please link to it. If you find that the designated duplicate lacks a particular variant of the standard answers, feel free to add it there. – tripleee May 09 '14 at 06:13

4 Answers4

2

Using awk:

awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]"

And with sed:

sed -n "/BUILD_VERSION/s/.*'\([^']*\)'.*/\1/p" input
perreal
  • 94,503
  • 21
  • 155
  • 181
  • This command works awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]" The values in the souce file i am getting from below command unzip -c work/$todeployname/warfile.war app/config/Property.js > /app/deployments/svnbuild.tmp . So instead of writing to a tmp file how can I directly feed that output of unizp - c to this command to get the build number awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]" – user1432622 May 14 '14 at 17:41
1
grep 'BUILD_VERSION' <your file> | sed -e 's/var BUILD_VERSION = //g'

Would get you '2014.17.10_23'; tweak the sed expression (or pipe it through a few more) to get rid of quotes.

It would be a 1 liner regex in Perl...

John3136
  • 28,809
  • 4
  • 51
  • 69
  • This command works awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]" The values in the souce file i am getting from below command unzip -c work/$todeployname/warfile.war app/config/Property.js > /app/deployments/svnbuild.tmp . So instead of writing to a tmp file how can I directly feed that output of unizp - c to this command to get the build number awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]" – – user1432622 May 14 '14 at 17:51
1

Here is another awk solution:

awk -F' = ' '/BUILD_VERSION/ {gsub(/\x27|;/,""); print $NF}'
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • This command works awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]" The values in the souce file i am getting from below command unzip -c work/$todeployname/warfile.war app/config/Property.js > /app/deployments/svnbuild.tmp . So instead of writing to a tmp file how can I directly feed that output of unizp - c to this command to get the build number awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]" – – user1432622 May 14 '14 at 17:51
  • @user1432622 pipe the output `your_command | awk ...` – jaypal singh May 14 '14 at 17:54
  • Yes but how do I refer the filename from your_command after the pipe in awk command? – user1432622 May 16 '14 at 23:48
0

You can use this awk

awk -F\' '/BUILD_VERSION/ {print $2}' file
2014.17.10_23
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • This command works awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]" The values in the souce file i am getting from below command unzip -c work/$todeployname/warfile.war app/config/Property.js > /app/deployments/svnbuild.tmp . So instead of writing to a tmp file how can I directly feed that output of unizp - c to this command to get the build number awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]" – – user1432622 May 14 '14 at 17:52
  • @user1432622 I am not familiar with how to get data from unzip. But you can remove the `tr` command. `awk -F= '/BUILD_VERSION/ {gsub(/[; \x27]/,"",$2);print $2}' input` The `\x27` creates the single quote `'` – Jotne May 14 '14 at 18:05