我有一个目录,其中许多SQL文件的代码如下(第1行)。我想将所有这些情况替换为第2行。有人可以建议我如何做到这一点吗?
第1行:
utl_file.Putf(g_logfile, '******This is sample log msg:' || '\n');
write_log(log_desc=> '****** This is sample log msg:', prc_name=>l_db_name);
答案 0 :(得分:1)
你的意思是这样的吗?
#!/bin/sh
# loop over all *.sql files
for file in *.sql; do
sed -i '
# if pattern match --> replace the rest of line
/utl_file.Putf(g_logfile,/ {s/ ||.*/, prc_name=>l_db_name);/}
# if pattern match --> replace head of line
s/utl_file.Putf(g_logfile,/write_log(log_desc=>/
' "$file"
done
使用您的sql文件在目录中启动此脚本。所有带有* .sql后缀的文件都将被处理。
答案 1 :(得分:1)
$ cat file
fooutl_file.Putf(g_logfile, '******This is sample log msg:' || '\n');bar
$ awk -i inplace '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]="" }
s=index($0,old) {
$0 = substr($0,1,s-1) new substr($0,s+length(old))
}
1' \
"utl_file.Putf(g_logfile, '******This is sample log msg:' || '\n');" \
"write_log(log_desc=> '****** This is sample log msg:', prc_name=>l_db_name);" \
file
$ cat file
foowrite_log(log_desc=> '****** This is sample log msg:', prc_name=>l_db_name);bar
以上代码使用GNU awk进行就地编辑,因此您可以通过一次调用awk:awk 'script' *
一次更改所有文件。与其他awk一起循环写入一个tmp文件:for f in *; do awk 'script' "$f" > tmp && mv tmp "$f"; done
。