【问题标题】:sed: unable to find and replace [duplicate]sed:无法找到和替换[重复]
【发布时间】:2018-07-10 04:45:52
【问题描述】:

我正在尝试用分配给名为 input 的变量的输入替换我的文本文件中的 customernumber。但无法使用 sed 完成此操作,以下是我正在尝试的方法

input=$(cat customernumber.txt)
echo $input
cat CUSTOMER_REPORT.txt | sed -e s/%customernumbers%/$input/g > temp.sql

我的customernumber.txt 包含值(12345,67890),我的CUSTOMER_REPORT.txt 包含以下值:

SELECT CUSTOMERNUMBER,ORDERNUMBER
FROM ORDER WHERE CUSTOMERNUMBER IN %customernumbers% WITH UR;

我收到以下错误:

sed: -e expression #1, char 31: unterminated `s' command

如何将 %customernumbers% 替换为 $input 中的值?除了 sed 之外,还有其他可用的选项吗?

【问题讨论】:

标签: bash unix sed


【解决方案1】:

你应该把你的替换 sed 命令放在双引号之间

sed -e "s/%customernumbers%/$input/g" > temp.sql

调试了一下:

cat CUSTOMER_REPORT.txt | sed -e "s/%customernumbers%/(12345,67890)/g" > temp.sql 

工作正常,因此我们缩小了根本原因,直到我们使用 wc -l customernumber.txtcat -vTE customernumber.txt 找到文件 customernumber.txt

经过一番修复后,它工作正常。

【讨论】:

  • 尝试相同但得到相同的错误 cat CUSTOMER_REPORT.txt | sed -e "s/%customernumbers%/$input/g" > temp.sql sed: -e expression #1, char 31: unterminated `s' command
  • 好的,我们一起调试吧!!! ;-) 试试这个命令,让我知道它是否有效:cat CUSTOMER_REPORT.txt | sed -e "s/%customernumbers%/(12345,67890)/g" > temp.sql 如果它有效,则意味着问题不是来自 sed 命令本身,而是来自变量中包含的内容,(例如在customernumber.txt 文件的结尾
  • 是的,上面的工作 cat temp.sql SELECT CUSTOMERNUMBER,ORDERNUMBER FROM ORDER WHERE CUSTOMERNUMBER IN (12345,67890) WITH UR;
  • 好的,那么问题不是来自sed 命令,而是来自您的变量$input 我怀疑它末尾有一个EOL,您可以执行以下命令:@987654332 @ 和 wc -l customernumber.txt 并给我输入?
  • 很好,问题出在 customernumber.txt 文件上。我现在已经纠正了它,它按预期工作..谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-09-22
  • 2020-06-24
  • 2010-11-29
  • 1970-01-01
  • 2017-01-06
  • 2017-02-08
  • 2020-02-03
  • 2020-08-05
相关资源
最近更新 更多