user1651888
user1651888

Reputation: 463

sed does not print line when using address format

sed does not print line 1 to line 545.The result of the following command is empty resultfile.txt. Can someone say what should be the correct command?

RESULT=545;sed -n '1, $RESULT p' Configuration.txt > resultfile.txt

Upvotes: 0

Views: 2816

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207345

Use double quotes so that $RESULT is expanded:

RESULT=545;sed -n "1, $RESULT p" Configuration.txt > resultfile.txt

By the way, the following would be simpler:

head -$RESULT Configuration.txt > resultfile.txt

And, if your file is big, this will quit as soon as you reach line 545 and be more efficient:

sed ${RESULT}q Configuration.txt > resultfile.txt

Upvotes: 1

torek
torek

Reputation: 487745

The above is not a sed issue, but rather a shell issue: you used single quotes, which inhibit variable expansion.

$ echo '$PWD'
$PWD
$ echo $PWD
/tmp
$ echo "$PWD"
/tmp

Either no quotes at all, or double quotes, allows the shell to expand variables. (No quotes at all means the shell expands everything; double quotes inhibit globbing, redirections, and such.)

Upvotes: 2

Related Questions