Reading variable and deleting multiple lines with sed at same time
by pedropt from LinuxQuestions.org on (#5RE0G)
Hi , i have an issue here , i am building a script where i input the lines i want to delete from a file , however if is not done at same time then sed will not delete the line i want if i do it on a loop to delete .
Imagine this code :
Text file with line numbers to delete (The numbers are not always the same)
filename: lines.tmp
Quote:
If i send sed to delete line 3 on a loop then when it goes for line 5 the content of that line is already in line 4 because line 3 was delete previously .
Based on that next this code
Code:var1=$(wc -l lines.tmp | awk '{print$1}')
for i in $(seq "$var1")
do
var2=$(sed -n ${i}p lines.tmp)
# out.file is the file where lines will be deleted
sed -i -e ${var2}d out.file
doneIf i use this code then sed will only delete correctly the first file but all others will be incorrect .
Is there a way to write this code to make sed delete everything on first run .
There is also an alternative for this to run well , witch is deleting from last number line to backwards , i think .
I would like some opinions if there is a better alternative to do it .
Thank you
Imagine this code :
Text file with line numbers to delete (The numbers are not always the same)
filename: lines.tmp
Quote:
3 5 2 79 23 |
Based on that next this code
Code:var1=$(wc -l lines.tmp | awk '{print$1}')
for i in $(seq "$var1")
do
var2=$(sed -n ${i}p lines.tmp)
# out.file is the file where lines will be deleted
sed -i -e ${var2}d out.file
doneIf i use this code then sed will only delete correctly the first file but all others will be incorrect .
Is there a way to write this code to make sed delete everything on first run .
There is also an alternative for this to run well , witch is deleting from last number line to backwards , i think .
I would like some opinions if there is a better alternative to do it .
Thank you