Shell script help is needed

I have a file test.txt and i need to grep pattern "A.17" from that file.

I know cat test.txt | grep A.17 will return the pattern, but it is returing like

# VERSION=A.17

How can i take only A.17 from this

if A.17 is found,
... do something
if not found
... do something

Please help me out to achive this ...

Thanks in advance
Renjesh Raju

Try this,

[[ `grep -cw 'A.17' inputfile` -gt 0 ]] && echo "found" || echo "not found"

cat filename | grep A.17

funny =)))

From the below pattern

# VERSION=A.17

how can i take only A.17 using "AWK" or any other utility ...

try as..

echo '# VERSION=A.17' | grep -o 'A.17'
or
grep -o 'A.17' inputfile
or by awk
echo '# VERSION=A.17' | awk 'sub(".*=","",$0)'

cat file_name | grep A.17 | sed "s/# VERSION=//g"

if version=$(grep 'VERSION=A\.17' infile);then
  echo "found version ${version#*=}"
else
  echo "version not found"
fi

avoid scanning the whole file if the pattern is found.

if version=$(sed '/VERSION=A\.17/q' infile);then
  echo "found version ${version#*=}"
else
  echo "version not found"
fi

@ctsgnb I think that should be

sed '/VERSION=A\.17/!d;q' infile

But sed does not return an error when there is no match...

@Scruti
Yep you're right !
I did my test on bad file... oops :slight_smile:

Though I think that Scritinizer's version is best, here is another one:

grep -q 'VERSION=A.17' test.txt ; REPLY=$?
if [ ${REPLY} -eq 0 ]
then
        echo "string found"
        echo "do something"
else
        echo "string not found"
        echo "do something else"
fi