SPD
SPD

Reputation: 1

How to use variable in Linux sed command

I have already read some topic but there is no similar to mine.

I need to pass to sed command with a $value but its too hard :

sed "1,$valued" filename.txt

like

sed "1,4d" filename.txt

How can i do that ?

Upvotes: 0

Views: 54

Answers (2)

Sobrique
Sobrique

Reputation: 53478

That's trying to use a variable called $valued.

So you can either:

1,${value}d.

Or set $valued to 4d.

Upvotes: 2

alexis
alexis

Reputation: 50190

Easiest way:

sed "1,$value"d filename.txt

$value is a shell variable (I assume you were already doing that). So you just needed a way to separate the variable name from what comes next (to avoid evaluating $valued). Quote marks do the job just fine since the shell removes them before calling sed.

Upvotes: 0

Related Questions