PurrBiscuit
PurrBiscuit

Reputation: 552

Stripping output of grep using regular expressions

I have a bash script I'm trying to tweak to output the contents of grep a certain way. Here's what my script looks like right now:

files=/var/chef/cache/cookbooks/*

for f in $files
  do
    echo "${f##*/}" && sudo cat $f/metadata.json | grep \"version\"
  done

output looks like this right now:

elasticsearch
  "version": "0.3.10"

I'd like to just have it output the actual number without the "version": in front or the quotes. The above example I'd like to look like this.

elasticsearch
  0.3.10

I'm new to regular expressions so I'm not sure what the best way to do this would be. Would it be useful to pipe the output from grep to use the sed utility with regex search? Any help would be much appreciated.

Upvotes: 1

Views: 94

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

echo "${f##*/}" && sudo cat $f/metadata.json | sed -n '/"version": "\([^"]*\)"/ s//\1/p'

filter and reformat directly in sed

Upvotes: 1

anubhava
anubhava

Reputation: 784928

You can replace echo command inside for loop by this:

echo "${f##*/}" && sudo awk -F ': *' '{gsub(/"/, "", $2); print $2}' $f/metadata.json

Upvotes: 1

Related Questions