sed循环中的参数 [英] Arguments inside sed loop
问题描述
我正在尝试了解sed命令和循环.我需要输入部分文本(20行)并将其附加到具有文件名的csv中.这是我的代码
Im trying to understand sed command and loops. I need to take part of the text (20 lines) and append it to the csv with the file name. here is my code
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中的文件名.
the problem is that my second "i" argument doesn't convert to the file name in csv.
在线
sed -e '1s/^/"$i" ;/' auto1.txt > auto2.txt
请告诉我我的错误是什么?
Please tell what is my mistake here?
推荐答案
问题是变量不会在单引号'
中扩展.使用双引号代替"
:
The issue is that variables are not expanded within single quotes '
. Use double quotes instead "
:
# this is assuming you did want to add explicit " around your $i
sed -e "1s/^/\"$i\" ;/" auto1.txt > auto2.txt
如果双引号字符串需要包含显式双引号,则必须转义它们( \"
).如果不能使用双引号(例如,如果您有非常复杂的命令)已经具有多个级别的引用),您随时可以退出变量周围的引用:
If your double-quoted string needs to contain explicit double quotes, you have to escape them (\"
). If using double quotes is not an option (e.g. if you have a very complex command with multiple levels of quoting already) you can always exit the quoting around your variable:
sed -e '1s/^/"'$i'" ;/' auto1.txt > auto2.txt
这篇关于sed循环中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!