LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   read line numbers , delete those lines and the previous ones with sed (https://www.linuxquestions.org/questions/programming-9/read-line-numbers-delete-those-lines-and-the-previous-ones-with-sed-4175632174/)

pedropt 06-18-2018 11:53 AM

read line numbers , delete those lines and the previous ones with sed
 
Hi , i am having issue to delete specific lines directly to a file

from a filtered file i get these line numbers i want to delete :
Quote:

8
10
12
14
16
20
2242
2244
2245
2249
2251
2253
2255
But i not only want to delete those as also the previous line of each number .

By this i mean these lines deleted :
Quote:

7
8
9
10
11
12
13
14
15
16
19
20
2241
2242
2243
2244
etc...
to delete my variable lines i can do a count of number of lines in variable , then do a loop with "for i until cunt of all lines"
like this :


My file content with the lines i want to delete from another file full of text :
Quote:

8
10
12
14
16
20
2242
2244
2245
2249
2251
2253
2255
lndel = file with the line numbers i need to delete in the outfile witch contains a lot of text

Code:

#Here it counts how many lines need to be deleted
lnempu=$(wc -l < lndel)

# start a loop from 1 to the number of lines to be deleted
for ((i=1; i<="$lnempu"; i++))
do

# here it read line number in i
lnvar=$(sed -n "${i}p" lndel)

# here sets another variable where decreases a number from the #previous var ex: if it read 55 from 1st line in lndel file then this #variable will be 54 , this way i am able to delete the line i want #and the previous one

prevl=$((lnvar-1))

# here sed cleans the previous line
sed -i -e '${prevl}d' outfile

# here sed cleans the current target line
sed -i -e '${lnvar}d' outfile
done
fi

However , sed is not deleting directly in file and i have no idea why .

pan64 06-18-2018 12:06 PM

Code:

sed -i -e '${prevl}d' outfile
you may need to use " instead of ' here.

But you need to take into account during delete the line numbering will change.

MadeInGermany 06-18-2018 04:02 PM

Note that your for loop reads the entire file n+1 times.
The following is more efficient
Code:

while IFS= read -r lnvar
do

# here sets another variable where decreases a number from the #previous var ex: if it read 55 from 1st line in lndel file then this #variable will be 54 , this way i am able to delete the line i want #and the previous one

  prevl=$((lnvar-1))

# here sed cleans the previous line and the following line
  sed -i "$prevl,+1d" outfile

done < lndel

The ,+1 includes the following line, thus avoiding another reading/writing of the outfile and also avoiding the previously mentioned problem with changed line numbers.

scasey 06-18-2018 04:43 PM

Quote:

Originally Posted by pedropt (Post 5868958)
However , sed is not deleting directly in file and i have no idea why .

Hmmm. Does the user running the command have write permissions on the file?
Confirm that the version of sed you're using supports the -i [--in-place] option.
Try putting the -i option last to be sure the following argument isn't being seen as a SUFFIX...(shouldn't)

Also, further to pan64's comment: Sort the file of line numbers inversely (largest to smallest), then delete largest first. That should avoid the line number changing issue.

PS You are making a backup of the outfile first, yes? ;)

pedropt 06-19-2018 03:54 PM

Both codes are working perfect .
However all i had to do was to remove the same line number twice , because deleting the previous one would change the file itself .

Basically is something like this :
Code:

#Here it counts how many lines need to be deleted
lnempu=$(wc -l < lndel)

# start a loop from 1 to the number of lines to be deleted
for ((i=1; i<="$lnempu"; i++))
do

# here it read line number in i
lnvar=$(sed -n "${i}p" lndel)

# here sets another variable where decreases a number from the #previous var ex: if it read 55 from 1st line in lndel file then this #variable will be 54 , this way i am able to delete the line i want #and the previous one

prevl=$((lnvar-1))

# here sed cleans the previous line
sed -i -e "${prevl}d" outfile
sed -i -e "${prevl}d" outfile
done
fi



All times are GMT -5. The time now is 02:00 AM.