0
/server/fmw/idm_111/opatch2019-01-25_08-20-55AM_1.log 
/server/fmw/oamwebgate11g/opatch2019-01-25_08-20-57AM_1.log 
/server/fmw/ohs_111/opatch2019-01-25_08-20-59AM_1.log 
/server/fmw/oracle_common/opatch2019-01-25_08-21-01AM_1.log

above is the content of a string array in a shell, want to get the line which has ohs_111

sporty
  • 51
  • 3

3 Answers3

2

grep - print lines matching a pattern.

For example:

grep -F ohs_111 file_with_content

Also grep could be used in script:

#! /usr/bin/env bash

array=("/server/fmw/idm_111/opatch2019-01-25_08-20-55AM_1.log"
    "/server/fmw/oamwebgate11g/opatch2019-01-25_08-20-57AM_1.log"
    "/server/fmw/ohs_111/opatch2019-01-25_08-20-59AM_1.log"
    "/server/fmw/oracle_common/opatch2019-01-25_08-21-01AM_1.log")

for line in ${array[@]}; do
    grep ohs_111 <<< $line
done

Output:

/server/fmw/ohs_111/opatch2019-01-25_08-20-59AM_1.log
Ilya G
  • 329
  • 3
  • 8
  • Why the `.*` ? You can remove them. Without `-x` option to grep it doesn't match the whole line exact, only part of it. – KamilCuk Jan 25 '19 at 10:51
1

see the example below:

#! /usr/bin/env bash
array=("hi...." "..foo/.." "..bar/..")
for i in ${array[@]}; do
    if [[ "$i" =~ "foo" ]] ; then
        echo "$i"
    fi
done

output:

..foo/..
Ilya G
  • 329
  • 3
  • 8
Kent
  • 189,393
  • 32
  • 233
  • 301
0

as it is explained in How to check if a string contains a substring in Bash for instance doing :

#!/bin/bash

array=("/server/fmw/idm_111/opatch2019-01-25_08-20-55AM_1.log" 
       "/server/fmw/oamwebgate11g/opatch2019-01-25_08-20-57AM_1.log" 
       "/server/fmw/ohs_111/opatch2019-01-25_08-20-59AM_1.log"
       "/server/fmw/oracle_common/opatch2019-01-25_08-21-01AM_1.log" )

for i in ${!array[*]}
do
  if [[ "${array[$i]}" == *"ohs_111"* ]]
  then
    echo "${array[$i]}"
  fi
done
bruno
  • 32,421
  • 7
  • 25
  • 37