Reputation: 1257
I am writing a script to delete a line of text file in ubuntu, my script asks the user which line they want to delete and store the line number to a variable. However, I don't know how to use sed with variable, I have searched many topic about sed. Unluckily, there are only thing like this:
sed '2d' file1 > file2
Which I want is
read index
# I have tried:
sed ($index)d file1 > file2 # didn't work
sed $(index)d file1 > file2 # didn't work
How to solve this?
Upvotes: 1
Views: 127
Reputation: 44043
Using a user-defined variable in sed code is asking for trouble -- sed cannot tell that you meant to put a number there if the user enters s/^/rm -rf \//e;
. Instead use awk:
awk -v line="$index" 'NR != line' filename
If you really want to do it with sed,
sed "${index}d" filename
will work as long as the user doesn't try to mess with you. If the user messes with you, it will explode in horrible ways.
Upvotes: 1