Shell 如何使用sed或任何其他命令替换SQL语句中的一行?
我们有几个Shell 如何使用sed或任何其他命令替换SQL语句中的一行?,shell,Shell,我们有几个.sql文件,其中一些文件有一个where子句来获取选择性数据。我想参数化将传递到select语句的值 例如: 文件1: select eid,deptid,name from employee where deptid = 25; 文件2: select studentname,studentid from student; 文件3: select staff,deptid,name from staff,dept where dept.id = staff.deptid an
.sql
文件,其中一些文件有一个where
子句来获取选择性数据。我想参数化将传递到select
语句的值
例如:
文件1:
select eid,deptid,name from employee where deptid = 25;
文件2:
select studentname,studentid from student;
文件3:
select staff,deptid,name from staff,dept where dept.id = staff.deptid
and deptid =25
我需要做以下几件事:
deptid上是否存在过滤条件
deptid
都应该被替换为28
readsqlfile.sh 28
用于file1 file2 file3 fileX中的i;do sed-i“s/deptid/${INPUT}/g”$i;完成
这将用
file1 file2 file3 fileX中INPUT
变量内的值替换所有出现的deptid
,您可以在文件替换中执行以下操作:
ishan@shurima:/tmp$ cat 1
select eid,deptid,name from employee where deptid = 25;
ishan@shurima:/tmp$ sed -i 's/where deptid/where 28/g' ./1
ishan@shurima:/tmp$ cat 1
select eid,deptid,name from employee where 28 = 25;
ishan@shurima:/tmp$
这只会改变deptid的情况。您是否尝试自己解决问题?是否确实要用参数替换列名或用参数替换列的值?谢谢Zelnes。它对我有用。。。我用下面的命令。