Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-09-2008, 03:34 PM   #1
LQ Newbie
Registered: Dec 2007
Posts: 1

Rep: Reputation: 0
how to delete two adjecent lines in a text file

hello, experts,
I need to delete the lines containing letter "a" and the next line together from a tect file.
I found the two lines with
fgrep a -A 1 filename
But I do not know how to delete them.

Please help me,

Thank you in advance,
Old 01-09-2008, 07:39 PM   #2
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
awk '/a/{getline;next}1' file
Old 01-10-2008, 01:35 PM   #3
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 77
Originally Posted by ghostdog74 View Post
awk '/a/{getline;next}1' file
The problem with this sort of approach is that it removes two lines without applying the pattern match to next itself. This might cause trouble when you have an even number of consecutive lines containing the character a in your file.

For example, if file is the following:
Your filter will result in the following output:
$ awk '/a/{getline;next}1' file
But the desired output would be:
It is unclear (to me anyway) if the OP is affected by this problem.
Old 01-10-2008, 02:23 PM   #4
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
sed -e '/a/,2D' <filename>
will delete all lines with 'a' in them, but the first instance will also delete the following line.

Last edited by bigrigdriver; 01-10-2008 at 02:31 PM.


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
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
Delete first and last lines of a file ChainsawPenguin Programming 5 09-28-2007 07:28 AM
How to Delete nnn lines from the top of a file with out opening it? rhelpm Linux - Newbie 6 12-07-2006 02:46 PM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
delete some lines from a file freelinuxcpp Linux - Software 4 01-17-2004 10:28 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:28 PM.

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