用数组元素替换每次出现的char

时间:2017-11-13 08:28:06

标签: shell unix

我有一个包含数据的文件:

varchar(50)

和一个String变量:

bq --schema
corp:STRING,                                               
region:STRING,                                                                                      
load_date_time:TIMESTAMP)                                      
bq --schema
corp:STRING,                                                                                          
load_date_time:TIMESTAMP) 

我想要做的是取代')'到数组元素,最终输出应如下所示:

table1

table2

字符串变量从以下文件生成:

bq --schema
corp:STRING,                                               
region:STRING,                                                                                      
load_date_time:TIMESTAMP table1                                  
bq --schema
corp:STRING,                                                                                          
load_date_time:TIMESTAMP table2

用于提取我在下面命令中使用的表名 CREATE TABLE UK_CHAIN_PARM_ADDL_FUNDING( corp:STRING, region:STRING, prin:STRING, assc:STRING, chain:STRING, acct_ind:STRING, transit_nbr:INTEGER, dda_nbr:STRING, currency:STRING, mask:STRING, value_dt:INTEGER ) CREATE TABLE UK_SUPPLEMENTAL_FUNDING( merchant:STRING, acct_ind:STRING, transit_nbr:INTEGER, dda_nbr:STRING, currency:INTEGER, mask:STRING, value_dt:INTEGER, beneficiary:STRING, vbc_line_1:STRING ) CREATE TABLE UK_REGION_PARM( corp:STRING, region:STRING, end_point:STRING, currency_code:STRING, tax_id1:STRING, tax_id2:STRING, load_date_time:TIMESTAMP)

所以最终我希望以格式输出

Tablenames=$(grep -i 'create table' $myfilename | awk -F ' ' 'BEGIN{ORS="\n";}{print $3}' | tr "(" "\n")

1 个答案:

答案 0 :(得分:1)

最后我找到了答案。因此,我没有使用数组,而是将字符串值放在具有新行分隔的文件中。

为实现预期输出而编写的代码:

while read line1
do
found=$(echo $line1 | grep -c ")")
if [ $found != 0 ];
then
while read line2
      do
           alreadycontainTable=$(grep -c "$line2" NewFileWithTableName.txt)
        if [ $alreadycontainTable == 0 ];
           then
           tableName=$(echo $line2)                       
           echo $line1 | sed -r "s/\)/ -t $tableName/" >> NewFileWithTableName.txt
          break;
        fi
            done < table.txt
else
           echo $line1 >> NewFileWithTableName.txt
fi
done < fileWithBrackets.txt