即使在 bash 脚本中使用双引号,SED 扩展也不起作用

如何解决即使在 bash 脚本中使用双引号,SED 扩展也不起作用

在使用 bash shell 脚本时遇到了一些麻烦。 基本上,我将值插入到一个文件中,然后从中创建一个变量,并尝试通过 sed 在另一个文件中附加和扩展变量值。

即:

cat animal.txt
Dog: 5
ferret: 1
Cat: 10
Hamster: 2
NUM=$(cat animal.txt)

然后我想将变量“NUM”中的值附加到另一个文件 temp.txt:

cat temp.txt
country: England  city: Manchester
country: England  city: Hull
country: England  city: Liverpool
country: England  city: London

尝试了所有这些变化,但都不够:

sed 's/$/'"${NUM}"'/' temp.txt
sed 's/$/"${NUM}"/' temp.txt
sed 's/$/'"${NUM}"'/' temp.txt
sed "s/$/"${NUM}"/" temp.txt
sed "s/$/\${NUM}\/" temp.txt

这两个有点工作,但变量仍然没有扩展:

sed 's/$/"${NUM}"/' temp.txt
sed 's/$/"\${NUM}\"/' temp.txt
country: England  city: Manchester  "${NUM}"
country: England  city: Hull  "${NUM}"
country: England  city: Liverpool  "${NUM}"
country: England  city: London  "${NUM}"

即使我将整个表达式用双引号括起来:

sed "s/$/${NUM}/" temp.txt
sed "s/$/\${NUM}\/" temp.txt

我明白了:

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

期望输出

country: England  city: Manchester  Dog: 5
country: England  city: Hull        ferret: 1
country: England  city: Liverpool   Cat: 10
country: England  city: London      Hamster: 2

country: England  city: Manchester  Dog:     5
country: England  city: Hull        ferret:  1
country: England  city: Liverpool   Cat:     10
country: England  city: London      Hamster: 2

我知道我应该避免使用单引号并使用双引号,但我错过了什么? 在这里使用 sed 是错误的工具吗?你认为 awk 会更好吗?

谢谢。

解决方法

在这里使用 sed 是错误的工具吗? 可能是的。

无论如何,如果您的 animals.txt 文件足够小,您可以使用此语句。

$ NUM="$(cat animal.txt)"; IFS=$'\n'; n=1; for num in $NUM; do i=0; while read -r line; do i=$((i + 1)); if [ $i -eq $n ]; then echo "$line" "$num"; n=$((n + 1)); break; fi; done < temp.txt; done

country: England  city: Manchester Dog: 5
country: England  city: Hull Ferret: 1
country: England  city: Liverpool Cat: 10
country: England  city: London Hamster: 2

但是对于大文件,awk 可能是更好的选择。

$ awk 'NR==FNR {num[FNR]=$0;next}{print $0 " " num[FNR]}' animal.txt temp.txt

country: England  city: Manchester Dog: 5
country: England  city: Hull Ferret: 1
country: England  city: Liverpool Cat: 10
country: England  city: London Hamster: 2

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?