将变量传递给sed for while循环

将变量传递给sed for while循环

问题描述:

我想使用shell sed循环从第n行开始的文件,我可以使它适用于整数(下面的例子从第5行开始),但是我想用sys参数变量替换该整数: linesToBegin = $ 1

I want to loop over a file starting from the n th line using shell sed,I could make it work for an integer (example below starting from line 5), but I want to replace the integer by an sys argument variable : linesToBegin= $1

这是代码:

sed -n '5,$ p' twitter_ids.txt | while read linecontent
do
            echo $linecontent 
done

当我用linesToBegin替换数字5时:

When I replace the number 5 by linesToBegin:

sed -n '$linesToBegin,$ p' twitter_ids.txt | while read linecontent

我说一个错误

sed: -e expression #1, char 3: extra characters after comman

linesToBegin="$1"
sed -n "$linesToBegin,\$ p" twitter_ids.txt | while read linecontent
do
            echo "$linecontent"
done

现在应该可以工作了.只需双引号即可.

Should probably work now. Just need double quotes.