Using variable output in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using variable output in awk
# 1  
Old 10-07-2019
Using variable output in awk

Hi,

I am trying to use variable output in awk to append a string to a word in a line. But that is not happening. Could you please help me on this.



The below is the code
Code:
#!/bin/ksh
set -x
src=/users/oracle/Temp
dst=/ora49/dpdump/$1
if [ "$#" -ne 3 ]; then
  echo "Usage: Script_Name DB_Name Parfile EXP|IMP"
  exit 1
fi
if [ ! -d "$dst" ]; then
    echo "Source path: $dst doesn't exist"
    exit 1
fi
echo $1
var1=$(echo "$1" | tr '[:upper:]' '[:lower:]')
var2=$(echo "$2" | tr '[:upper:]' '[:lower:]')
var3=$(echo "$3" | tr '[:upper:]' '[:lower:]')
echo $var1
ORACLE_SID="$var1"
ORAENV_ASK=NO
. oraenv 2>&1
ORAENV_ASK=YES
if [ `ps -ef | grep smon | grep $var1 | awk -F "_" '{print $3}'` == $var1 ]; then
        if [ -f "$src/$var2" ]; then
                echo " Parfile exists"
                cp "$src/$var2" "$dst"
                chmod 700 "$dst/$var2"
                if [ "$var3" == "exp" ]; then
                        echo "Export"
                        if [[ `cat "$dst/$var2" | sed -e 's/[\t ]//g;/^$/d' | awk -F "=" 'tolower($1)=="userid"{print $2}'` == "appdbinstall" ]]; then
                                echo "appdbinstall exists"
                                vpd=`cat $src/encrypted.pwd`
                                echo "$vpd"
                                awk '/appdbinstall/ {$0=$0;for (i=1;i<NF;i++)t=t ;$0=$0 "/${vpd}"}1' "$dst/$var2" >> "$dst/${var1}_tmp.par"
								nohup expdp parfile="$dst/${var1}_tmp.par" &
                        else
                                echo "appdbinstall not exists"
                        fi
                elif [ "$var3" == "imp" ]; then
                        echo "Import"
                else
                        echo "Pass 3rd parameter EXP|IMP"
                fi
        else
                echo "Parfile does not exist"
        fi
else
        echo "Please pass value for DB name or DB is not running"
        exit 1
fi


Code:
$ cat test_10062019.par
userid = appdbinstall
job_name = test_export
tables = dbadmin.gg_test
directory = EXPDP
dumpfile=test_export.dmp
logfile=test_export.log

What I am trying to achieve is if userid in parfile is appdbinstall, i need to attach the password from encrypted.pwd like , userid = appdbinstall/<password>.

Code:
awk '/appdbinstall/ {$0=$0;for (i=1;i<NF;i++)t=t ;$0=$0 "/${vpd}"}1' "$dst/$var2" >> "$dst/${var1}_tmp.par"

:- This is not working . Could you please suggest on this.

Thanks,
Mani
Old 10-07-2019
A short but proper description the entire situation (how the script is called, what the positional parameters stand for, how parfile and test_10062019.par relate) would help people understand the problem and help you get faster and better answers. Meaningful indentation would improve readability and understandibility of your code.


Use the correct mechanism to convey a shell variable's contents into an awkscript, like
Code:
awk -vPWD=$vpd '/appdbinstall/ {$0=$0;for (i=1;i<NF;i++)t=t ;$0=$0 "/" PWD}1' file


You might want to try sth. like this on your way to optimize your script:
Code:
awk '
NR == 1         {PWD = $0
                 next
                }
/appdbinstall/  {$NF = $NF "/" PWD
                 CHD = 1
                }
1
END             {if (CHD) print "appdinstall exists." > "/dev/stderr"
                 exit (!CHD)
                }
'  encrypted.pwd test_10062019.par

Check for the exit code to control further processing.
# 3  
Old 10-07-2019
Sorry RudiC. Its my bad that i dint give a description about the code. I will definitely do this next time.

This script accepts 3 arguments ie, DB name, parfile and EXP|IMP.
This script will be used by application team and they will not able to access as sys user. That is why we are providing access to another user called appdbinstall. Right now password is stored in plain text and get the same during execution and attach to the userid value . I am trying to figure out some way to encrypt/decrypt password. But crypt is not installed in our system.

I will try your suggestion and let you know.

Thanks,
Mani

Last edited by pvmanikandan; 10-07-2019 at 11:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo awk output from its variable

Stumped with the formatting of the awk output when used with variables, e.g.: awk -F, 'BEGIN {OFS=","} print {$2,$3,$4}' $infile1 produces the desired output (with rows), but when echoing the variable below, the output is one continuous line var1=$(awk -F, 'BEGIN {OFS=","} print... (4 Replies)
Discussion started by: ux4me
4 Replies

2. Shell Programming and Scripting

How to read the variable from awk output?

I am reading an xml file with date tag as <Date>Default</Date> using the below command. Dt=$(awk -F'' '/<Date>/{print $3}' /home/test/try.xml and getting the value from the xml file stored in this variable "Dt" echo $Dt gives me a value. Dt=Default. Now according to my requirement, If... (2 Replies)
Discussion started by: Saidul
2 Replies

3. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

4. Shell Programming and Scripting

Assigning output from awk to variable

I have a script whose contents are as below result= awk 's=100 END {print s }' echo "The result is" $result The desired output is The result is 100 My script is running without exiting and i am also not getting the desired output. Please help (5 Replies)
Discussion started by: bk_12345
5 Replies

5. Shell Programming and Scripting

help on awk---- need to assign the output of awk to a variable

hi i want to find the size of a folder and assign it to a variable and then compare if it is greater than 1 gb. i am doin this script, but it is throwing error.... #!/bin/ksh cd . | du -s | size = awk '{print $1}' if size >= 112000 then echo size high fi ERROR : (4 Replies)
Discussion started by: Nithz
4 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

awk output in a variable

Not sure why it is not working the following : set -- $@ stype ="a" for shell_args in "$@" do $stype=` awk '{print substr ("'"$shell_args"'", 0, 3)}' ` echo $stype done Thank you (5 Replies)
Discussion started by: andaluzia
5 Replies

8. Shell Programming and Scripting

AWK Output into a variable

Hi I am trying to store the output of awk into a variable in a shell script. I can run it successfully from the command line but not from a ksh shell script. ls -al test.txt | grep -v grep | awk '{print $1}' returns -rw-r--r-- #!/bin/ksh perm=$(`ls -al test.txt | grep -v grep | awk... (2 Replies)
Discussion started by: mace_560
2 Replies

9. UNIX for Dummies Questions & Answers

Set a variable from awk output

I have a file which I am processing using awk to spit out the following: export CLIENT=1 ; export USER=1 ; export METABASE=1 ; export TASK=1 ; export TOTAL=3 What i want to do now is execute that within the script so those variables are available to other commands. I've tried piping the... (3 Replies)
Discussion started by: Cranie
3 Replies

10. UNIX for Dummies Questions & Answers

how to output awk to a variable

Hi folks, I am wondering how to output awk back to a variable. I am new to Unix/Linux. I am trying to get rid of a decimal number and put the output back in a variable for further use in the script. here is how I used awk: var=$1 echo $var |awk '{print $1 *100}' | $var echo $var this... (4 Replies)
Discussion started by: bashirpopal
4 Replies
Login or Register to Ask a Question