LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-05-2016, 05:32 AM   #1
arun natarajan
Member
 
Registered: Jun 2014
Posts: 111

Rep: Reputation: Disabled
sed deletes extra lines


hi,

when i am executing below sed command for deleting a single line which starts with "bin" and ends with "nologin". But it also deletes the next line from the matching pattern.


[root@node2 ~]# uname -a
Linux node2.cluster.com 2.6.32-71.el6.x86_64 #1 SMP Wed Sep 1 01:33:01 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

[root@node2 ~]# rpm -qa sed
sed-4.2.1-5.el6.x86_64

before executing the cmd...

[root@node2 ~]# cat -n arun | head
1 root:x:0:0:root:/root:/bin/bash
2 root:x:0:0:root:/root:/bin/bash
3 root:x:0:0:root:/root:/bin/bash
4 root:x:0:0:root:/root:/bin/bash
5 root:x:0:0:root:/root:/bin/bash
6 root:x:0:0:root:/root:/bin/bash
7 bin:x:1:1:bin:/bin:/sbin/nologin
8 daemon:x:2:2:daemon:/sbin:/sbin/nologin
9 sync:x:5:0:sync:/sbin:/bin/sync
10 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

after executing a command

[root@node2 ~]# sed -i /^bin/,/nologin$/d arun

[root@node2 ~]# cat -n arun | head
1 root:x:0:0:root:/root:/bin/bash
2 root:x:0:0:root:/root:/bin/bash
3 root:x:0:0:root:/root:/bin/bash
4 root:x:0:0:root:/root:/bin/bash
5 root:x:0:0:root:/root:/bin/bash
6 root:x:0:0:root:/root:/bin/bash
7 sync:x:5:0:sync:/sbin:/bin/sync
8 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 halt:x:7:0:halt:/sbin:/sbin/halt
10 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@node2 ~]#




it is suppose to delete only line number 7, but it also deletes line number 8, why ???

Last edited by arun natarajan; 09-05-2016 at 07:46 AM.
 
Old 09-05-2016, 05:56 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Because you told it to. Read the documentation
Quote:
If the second address is a regexp, then checking for the ending match will start with the line following the line which matched the first address: a range will always span at least two lines (except of course if the input stream ends).
 
1 members found this post helpful.
Old 09-05-2016, 06:28 AM   #3
arun natarajan
Member
 
Registered: Jun 2014
Posts: 111

Original Poster
Rep: Reputation: Disabled
ok got it.

but is there a way to delete a particular line alone, when lines are ending with same word.??
 
Old 09-05-2016, 06:45 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by arun natarajan View Post
... deleting a single line which starts with "adm" and ends with "nologin".
Your sample input file contains no lines which start with adm.

Daniel B. Martin
 
1 members found this post helpful.
Old 09-05-2016, 06:47 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by arun natarajan View Post
but is there a way to delete a particular line alone, when lines are ending with same word.??
Consider grep.

Daniel B. Martin
 
Old 09-05-2016, 07:09 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed is the correct tool to update in-place. Selecting the line to delete simply requires correct specification of the line to delete - regex usually makes that easier. Don't specify a line range - just use the command to select the data to delete.
 
1 members found this post helpful.
Old 09-19-2016, 07:05 AM   #7
jostber
Member
 
Registered: Jul 2001
Location: Skien, Norway
Distribution: Slackware Current 64-bit
Posts: 543

Rep: Reputation: 178Reputation: 178
This should work better:

Code:
sed -i /^bin.*nologin$/d arun
 
Old 09-19-2016, 07:38 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,800

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by syg00 View Post
sed is the correct tool to update in-place. Selecting the line to delete simply requires correct specification of the line to delete - regex usually makes that easier. Don't specify a line range - just use the command to select the data to delete.
/etc/password is a file which must not be modified in-place. Just to be sure you did not make any mistake, otherwise you will not be able to login any more....
(yes, I know it was a copy of that file)

Last edited by pan64; 09-19-2016 at 07:39 AM.
 
Old 11-06-2016, 01:42 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
@jostber
Quote:
Originally Posted by arun natarajan
Thanks for your answer. Its working as expected.
@arun natarajan
Returning to say thank you is good form and always appreciated, but apparently you clicked Report instead of Quote or Reply.

If your original question is now resolved please use the Thread Tools option at top of this thread to mark it Solved.

Thanks!
 
  


Reply



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] script deletes empy lines trintukaz Programming 3 10-01-2011 05:15 AM
deleting the selected extra lines adidassharma Programming 17 10-17-2008 07:10 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 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
Extra lines pforget Slackware 8 11-25-2005 10:18 AM

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

All times are GMT -5. The time now is 02:02 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