LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-09-2008, 03:34 PM   #1
wangxinmco
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
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Rep: Reputation: 79
Quote:
Originally Posted by ghostdog74 View Post
Code:
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:
Code:
a
ba
c
d
Your filter will result in the following output:
Code:
$ awk '/a/{getline;next}1' file
c
d
But the desired output would be:
Code:
d
It is unclear (to me anyway) if the OP is affected by this problem.
 
Old 01-10-2008, 02:23 PM   #4
bigrigdriver
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.
 
  


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
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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