LinuxQuestions.org
Help answer threads with 0 replies.
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 01-31-2007, 05:13 PM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
delete a line containing a pattern and the next line of a text file


How to delete a line containing a pattern and the next line of a text file?
Any programming language is ok.
Thanks.
e.g.
Delete the line containing the "PRE-AUTHORIZED PAYMENT" and the next (adjacent) line.
Input:
Dec. 29, 2006 Jan. 02, 2007
THE BRICK WAREHOUSE L.P
$632.70
Dec. 29, 2006 Dec. 29, 2006 PRE-AUTHORIZED PAYMENT
$1,291.95
Dec. 31, 2006 Jan. 02, 2007
PRODUCE DEPOT
$33.83

Output:
Dec. 29, 2006 Jan. 02, 2007
THE BRICK WAREHOUSE L.P
$632.70
Dec. 31, 2006 Jan. 02, 2007
PRODUCE DEPOT
$33.83
 
Old 01-31-2007, 05:24 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Perl is excellent for simple stuff like this. (grin)

You could probably do it like this:

Open the input file, and open an output file. Copy each line from input to output. Keep a line number for each input line; it would be 1 for the first line, 2 for the second, and so on. If an input line contains the pattern of interest, set another variable (let's call it $elephant) equal to the current line number; otherwise, leave $elephant alone. Then, before you output the current line, compare the current line number with $elephant. If it's equal to $elephant, or equal to $elephant+1, don't output this line.

For this to work, you'll need to set $elephant to something like -1 at the beginning; otherwise, the first line will not be output because the line number will be 1 and $elephant will be 0.

If you're not familiar with Perl, http://perldoc.perl.org is your friend. In particular, I'd recommend you visit:

http://perldoc.perl.org/index-overview.html
http://perldoc.perl.org/index-tutorials.html
http://perldoc.perl.org/index-faq.html

And when you're ready to eat the whole enchilada:

http://perldoc.perl.org/index-language.html

Hope this helps.

Last edited by wjevans_7d1@yahoo.co; 01-31-2007 at 05:27 PM.
 
Old 01-31-2007, 05:28 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Without changing original file:
Code:
sed '/PRE-AUTHORIZED PAYMENT/,+1 d' input_file > new_file
Or, in place editing:
Code:
sed -i '/PRE-AUTHORIZED PAYMENT/,+1 d' input_file
Sed is your friend.
 
Old 01-31-2007, 05:34 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Code:
gawk '/PRE-AUTHORIZED PAYMENT/ {getline ; getline}{print $0}' filename > new_filename
gawk is a good friend, too!
 
  


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
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM
Delete line from flat text file in C zaichik Programming 6 01-26-2005 06:16 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM


All times are GMT -5. The time now is 12:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration