0

Below is my sample test.log file

export SQRDIR=/v/orahome/Middleware/Oracle/bin64


 
export OID=ap0092

export PWD=pass1

export FDPWD=pass1

export AP0085_PWD=pass1

export SVR=AFFPROD

export TWO_TASK=db01

 

export EF_OID=AP0093

export EF_PWD=pass2

 

export CCC_PER_OID=CCC_JAS

export CCC_PER_PWD=pass3

 

export CAN_PER_OID=CAN_JAS

export CAN_PER_PWD=pass4

 

###################################################

The user inputs ap0092. Upon this input, I need to search for the first occurrence of PWD after the first occurrence of the search string i.e. =ap0092

Thus, the desired output will be pass1

Likewise, for CCC_JAS the desired output will be pass3

I need the solution for Solaris OS.

I was doing grep -A 1 [for non-Solaris] to get the next line but do not know how to search if the desired line may not be the very next line.

Please note: There may be multiple lines or Whiteline between export OID=ap0092 and

export PWD=pass1

Kindly suggest.

2
  • What did you try so far? Commented Dec 4, 2023 at 7:36
  • @RomeoNinov I was doing grep -A 1 [for non-solaris] to get the next line but do not know how to search if the desired line may not be the very next line. Commented Dec 4, 2023 at 8:08

1 Answer 1

0

Could be:

#! /bin/sh -
oid="${1?No OID specified}"
file=/path/to/your/file.log

die() {
  printf>&2 '%s\n' "$@"
  exit 1
}

password=$(
  OID=$oid perl -ne '
    if (/^export (\w+_)?OID=(.*)$/) {
      $prefix = $1 if $right_oid = $2 eq $ENV{OID};
    } elsif ($right_oid && /^export ${prefix}PWD=(.*)$/) {
      print "$1\n"; $found = 1; exit
    }
    END {exit !$found}' < "$file"
) || die "No password found for $oid"

printf '%s\n' "The password for $oid is $password"

If on Solaris 10 or older, replace #! /bin/sh with #! /usr/xpg4/bin/sh or #! /bin/ksh, as /bin/sh was not a POSIX shell back then but still on old SysV shell.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.