johnny-b-goode
johnny-b-goode

Reputation: 3893

Unix grep command

I have an utility script, that displays an information about deployed java app. Here is an example output of this script:

Name: TestAPP

Version : SNAPSHOT

Type : ear, ejb, webservices, web

Source path : /G/bin/app/TESTAPP_LIVE_1.1.9.1.1.ear

Status : enabled

Is it possible to grep Version and source path values using grep command? Right now im able to do this using following command:

| grep Version

But it outputs the whole string (e.g. Version: Snapshot) when i am need only a values (e.g Snapshot to use in further script commands)

Upvotes: 0

Views: 221

Answers (3)

Benjamin Toueg
Benjamin Toueg

Reputation: 10867

Here is a pure grep solution.

Use the -P option for regex mode, and -o option for retrieving only what is matching.

grep -Po "(?<=^Version : ).*"

Here is what you would do for Source:

grep -Po "(?<=^Source : ).*"

It uses a postive lookbehind.

Upvotes: 2

MMM
MMM

Reputation: 7310

Here's a solution using awk if you're interested:

grep Version | awk '{print $3}'

$3 means to print the third word from that line.

Note that:

  1. This displays one word only
  2. This assumes you have spaces between the colon (and therefore the version is actually the third "word"). If you don't, use $2 instead.

Upvotes: 0

Satya
Satya

Reputation: 8881

grep Version | cut -d ':' -f 2 

Upvotes: 2

Related Questions