我正在尝试理解 sed 命令和循环。 我需要获取部分文本(20 行)并将其附加到带有文件名的 csv 中。 这是我的代码
for i in ~/mspl/*.doc
do
catdoc "$i" > auto.txt
sed -e '1,20!d' auto.txt > auto1.txt
sed -e '1s/^/"$i" ;/' auto1.txt > auto2.txt
sed -e '20s/$/~/' auto2.txt > auto3.txt
cat auto3.txt >> lines.csv
done
问题是我的第二个“i”参数没有转换为 csv 中的文件名。
行内
sed -e '1s/^/"$i" ;/' auto1.txt > auto2.txt
请告诉我这里有什么错误?
最佳答案
问题是变量没有在单引号 '
内展开。使用双引号代替 "
:
# this is assuming you did want to add explicit " around your $i
sed -e "1s/^/\"$i\" ;/" auto1.txt > auto2.txt
如果您的双引号字符串需要包含明确的双引号,您必须将它们转义 (\"
)。如果不能使用双引号(例如,如果您有一个非常复杂的命令已经有多个级别的引用)你总是可以退出变量周围的引用:
sed -e '1s/^/"'$i'" ;/' auto1.txt > auto2.txt
关于linux - sed 循环中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14402121/