LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2018, 11:53 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
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 .
 
Old 06-18-2018, 12:06 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,630

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
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.
 
1 members found this post helpful.
Old 06-18-2018, 04:02 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,761

Rep: Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187
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.
 
1 members found this post helpful.
Old 06-18-2018, 04:43 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,701

Rep: Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208Reputation: 2208
Quote:
Originally Posted by pedropt View Post
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?
 
Old 06-19-2018, 03:54 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed: delete blank lines and modify line immediately after such porphyry5 Programming 5 11-25-2014 07:42 PM
Sed issue with finding a match on two sequential lines and deleting the previous line ulto Programming 3 10-26-2014 06:26 PM
sed multiple lines previous line gushnik1 Linux - Software 2 10-09-2013 03:15 AM
[SOLVED] How to use sed command to delete lines only 1-3 digit numbers on a particular column? kahthiam Linux - Newbie 4 12-27-2011 07:41 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration