Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何使用sed或任何其他命令替换SQL语句中的一行?_Shell - Fatal编程技术网

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
我需要做以下几件事:

  • 读取所有列出的文件。(即文件1、2和3)
  • 检查
    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。它对我有用。。。我用下面的命令。