我们可以使用shell变量AWK? [英] Can we use shell variables in awk?

查看:134
本文介绍了我们可以使用shell变量AWK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用AWK shell变量如 $ VAR 而不是 $ 1 $ 2 ?例如:

Can we use shell variables in AWK like $VAR instead of $1, $2? For example:

UL=(AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW)

NUSR=`echo ${UL[*]}|awk -F, '{print NF}'`
echo $NUSR
echo ${UL[*]}|awk -F, '{print $NUSR}'

其实是一个Oracle数据库管理员,我们得到很多进口的请求。我试图使用脚本来自动执行它。该脚本将找出用户在转储和提示到转储需要加载的用户。

Actually am an oracle DBA we get lot of import requests. I'm trying to automate it using the script. The script will find out the users in the dump and prompt for the users to which dump needs to be loaded.

假设堆放有两个用户 AKHIL SWATHI (可以有可能在转储用户,我想导入更多的用户的数量)。我想转储导入到新用户 AKHIL_NEW SWATHI_NEW 。因此,输入要读一些认为像 AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW

Suppose the dumps has two users AKHIL, SWATHI (there can be may users in the dump and i want to import more number of users). I want to import the dumps to new users AKHIL_NEW and SWATHI_NEW. So the input to be read some think like AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW.

首先,我需要找到要创建的用户数,那么我需要获得新用户即 AKHIL_NEW,SWATHI_NEW 从输入我们给予。这样我就可以连接到数据库并创建新的用户,然后导入。我不是复制整个code:我刚才复制从那里接受输入用户code

First, I need to find the Number of users to be created, then I need to get new users i.e. AKHIL_NEW,SWATHI_NEW from the input we have given. So that I can connect to the database and create the new users and then import. I'm not copying the entire code: I just copied the code from where it accepts the input users.

UL=(AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW) ## it can be many users like     USER1:USER1_NEW,USER2_USER2_NEW,USER3:USER_NEW..

NUSR=`echo ${UL[*]}|awk -F, '{print NF}'` #finding  number of fields or users
y=1
while [ $y -le $NUSR ] ; do
    USER=`echo ${UL[*]}|awk -F, -v NUSR=$y  '{print $NUSR}' |awk -F: '{print $2}'` #getting     Users to created AKHIL_NEW and SWATHI_NEW and passing to SQLPLUS
    if [[ $USER = SCPO* ]]; then
        TBS=SCPODATA
    else
        if [[ $USER = WWF* ]]; then
            TBS=WWFDATA
        else
            if [[ $USER = STSC* ]]; then
                TBS=SCPODATA
            else
                if [[ $USER = CSM* ]]; then
                    TBS=CSMDATA
                else
                    if [[ $USER = TMM* ]]; then
                        TBS=TMDATA
                    else
                        if [[ $USER = IGP* ]]; then
                        TBS=IGPDATA
                        fi
                    fi
                fi
             fi
        fi
    fi

    sqlplus -s '/ as sysdba'  <<EOF   # CREATING the USERS in the database 
    CREATE USER $USER IDENTIFIED BY $USER  DEFAULT TABLESPACE $TBS TEMPORARY TABLESPACE TEMP QUOTA 0K on SYSTEM QUOTA UNLIMITED ON $TBS;

    GRANT
    CONNECT,
       CREATE TABLE,
       CREATE VIEW,
       CREATE SYNONYM,
       CREATE SEQUENCE,
       CREATE DATABASE LINK,
       RESOURCE,
       SELECT_CATALOG_ROLE
    to $USER;
    EOF 
    y=`expr $y + 1`
done

impdp sysem/manager DIRECTORY=DATA_PUMP DUMPFILE=imp.dp logfile=impdp.log SCHEMAS=AKHIL,SWATHI REMPA_SCHEMA=${UL[*]} 

在过去的IMPDP命令我需要在垃圾场原来使用的用户变量即AKHIL,SWATHI。

In the last impdp command I need to get the original users in the dumps i.e AKHIL,SWATHI using the variables.

推荐答案

您code没有意义,但问题很容易回答:是的,你可以使用的 shell变量的内部AWK。有一帮做的方法,但我最喜欢的是定义与 -v 标志一个变量:

Your code does not make sense, but the question is easily answered: yes, you can use the shell variables inside awk. There are a bunch of ways of doing it, but my favorite is to define a variable with the -v flag:

$ echo | awk -v my_var=4 '{print "My var is " my_var}'
My var is 4

只是通过环境变量作为参数传递给了 -v 标记。例如,如果你有这样的变量:

Just pass the environment variable as parameter to the -v flag. For example, if you have this variable:

$ VAR=3
$ echo $VAR
3

使用这种方式:

$ echo | awk -v env_var="$VAR" '{print "The value of VAR is " env_var}'
The value of VAR is 3

当然,你可以把同一个名字,但 $ 不会是必要的:

$ echo | awk -v VAR="$VAR" '{print "The value of VAR is " VAR}'
The value of VAR is 3

有关注意 $ 在AWK 的:不同的bash,Perl和PHP等,它不是变量名的一部分,但而<一个href=\"http://stackoverflow.com/questions/11880654/does-awk-support-dynamic-user-defined-variables/11882535#11882535\">an运营商。

这篇关于我们可以使用shell变量AWK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆