sed deletion doubt

Asked by Wahan

Hello all..
plz help me.

I have a doubt regarding deletion usind sed. We can use following cammand to delete lines between 5 and 10 from filename.txt.

sed '5,10d' filename.txt

I have two variable $startline and $endline. How do use sed command with these variables? when i use
sed '$startline,endlined filename.txt
i am getting errors.
I know this is a basic syntax error, but plz help me to solve this.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu sed Edit question
Assignee:
No assignee Edit question
Solved by:
Wahan
Solved:
Last query:
Last reply:
Revision history for this message
actionparsnip (andrew-woodhead666) said :
#1

cat filename.txt | sed -e '$startline,$endlined' > result.txt

May just work

It may be:

cat filename.txt | sed -e '($startline),($endline)d' > result.txt

Bash is a funny ole game, I'd post on a bash forum and/or ask in #bash on irc.freenode.net

Maybe someone with bash-fu can contribute better

Revision history for this message
Wahan (abdulwahabkdi-gmail) said :
#2

Thnx actionparsnip .

I found a solution.
sed -i "$startline,endline d" filename.txt

it does work.

Anywyz thnx for the reply. i will check ur suggestion also.
regards

Revision history for this message
Ralph Corderoy (ralph-inputplus) said :
#3

Wahan, I think you're mistyping what works. endline has no dollar.

    sed -i "$start,$end d" foo
    sed -i "$start,${end}d" foo

These will both delete that range of lines in-place. In most shells ${var} can be used instead of $var when the following text would alter the variable name, e.g. to $vard.