Passing variable to sed for while loop [duplicate]

2020-05-09 17:56发布

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

Here is the code:

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

When I replace the number 5 by linesToBegin:

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

I get an error saying

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

1条回答
神经病院院长
2楼-- · 2020-05-09 18:29
linesToBegin="$1"
sed -n "$linesToBegin,\$ p" twitter_ids.txt | while read linecontent
do
            echo "$linecontent"
done

Should probably work now. Just need double quotes.

查看更多
登录 后发表回答