如何用其他文件的内容替换文件中的字符串

时间:2017-01-31 16:06:46

标签: unix awk sed

我正在AIX使用ksh,我需要一些帮助,因为我无法通过sedawk实现这一目标。 模板文件:(用作输入)

...  
varchar SelectStmt = 'REPLACE_SELECT_STATEMENT'
...

应该应用的值(根据Teradata中表名的输入派生并分别比较每列的列数据类型并在必要时将其投射到上面)通过以下方式实现awk并重定向到一个文件,该文件有许多行(> =&100;< = 500)。

我正在读取该文件,需要将文件内容替换为Template文件的上述语句。我试过了

v_select_stmnt=`cat awk_output_file.txt`;   
 sed 's/REPLACE_SELECT_STATEMENT/"$v_select_stmnt"/g' 
                   Template_file.txt > /directory1/final_script.txt;

甚至尝试awk作为

 awk -v a_select_stmnt="$v_select_stmnt" '
 { if ($0 ~ /REPLACE_SELECT_STATEMENT/) { 
        sub(REPLACE_SELECT_STATEMENT,a_select_stmnt,$0); }
        print $0 > "/directory1/final_script.txt" }' Template_file.txt

awk_output_file.txt的示例内容:(可能有单引号,分号)

SELECT COLUMN1, CAST(COLUMN2 AS DECIMAL(10,3)),
       CAST(COLUMN3 AS VARCHAR(40)), COLUMN4,
       ....
       CAST(COLUMN600 AS INTEGER) 
FROM DATABASE1.TABLE1 WHERE COLUMN_DATE = '2017-01-31';

预期输出

...  
    varchar SelectStmt = 'SELECT COLUMN1, CAST(COLUMN2 AS DECIMAL(10,3)),
           CAST(COLUMN3 AS VARCHAR(40)), COLUMN4,
           ....
           CAST(COLUMN600 AS INTEGER) 
    FROM DATABASE1.TABLE1 WHERE COLUMN_DATE = '2017-01-31';'
 ...

你能帮我解决一下吗?由于select语句可能很大,因此需要保留new-line个字符以使其可读。如果可能,请在sedawk中发布如何解决此问题。谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

模板文件

$ cat temp 
...  
varchar SelectStmt = 'REPLACE_SELECT_STATEMENT'
...

内容

$ cat output 
SELECT COLUMN1, CAST(COLUMN2 AS DECIMAL(10,3)),
       CAST(COLUMN3 AS VARCHAR(40)), COLUMN4,
       ....
       CAST(COLUMN600 AS INTEGER) 
FROM DATABASE1.TABLE1 WHERE COLUMN_DATE = '2017-01-31';

<强> awk中

  

假设您的输出文件不包含&,因为它将被解释   通过更换操作

$ awk 'NR==FNR{r=(FNR>1?r RS:"") $0; next} {gsub(/REPLACE_SELECT_STATEMENT/,r)}1' output temp 

更好的可读版本

awk '  # Here read output file and save contents in variable r
       NR==FNR{
            r=(FNR>1?r RS:"") $0
            next
        }

        # Here read file temp
        {
           gsub(/REPLACE_SELECT_STATEMENT/,r)
        }1
   ' output temp 
  

如果它在输出文件中包含&个字符,那么

awk 'BEGIN{search="REPLACE_SELECT_STATEMENT"}NR==FNR{r=(FNR>1?r RS:"") $0; next} s=index($0,search){ $0 = substr($0,1,s-1) r substr($0,s+length(search)) }1' output temp

更好的可读版本

awk 'BEGIN{
        search="REPLACE_SELECT_STATEMENT"
}
NR==FNR{
        r=(FNR>1?r RS:"") $0
        next
} 
# To take care of & char, credit : Ed Morton
s=index($0,search){ 
        $0 = substr($0,1,s-1) r substr($0,s+length(search)) 
}1' output temp

两者都提供输出

...  
    varchar SelectStmt = 'SELECT COLUMN1, CAST(COLUMN2 AS DECIMAL(10,3)),
           CAST(COLUMN3 AS VARCHAR(40)), COLUMN4,
           ....
           CAST(COLUMN600 AS INTEGER) 
    FROM DATABASE1.TABLE1 WHERE COLUMN_DATE = '2017-01-31';'
...

<强>的Perl

$ perl -pe 's/REPLACE_SELECT_STATEMENT/`cat output`/ge' temp
...  
varchar SelectStmt = 'SELECT COLUMN1, CAST(COLUMN2 AS DECIMAL(10,3)),
       CAST(COLUMN3 AS VARCHAR(40)), COLUMN4,
       ....
       CAST(COLUMN600 AS INTEGER) 
FROM DATABASE1.TABLE1 WHERE COLUMN_DATE = '2017-01-31';
'
...