LinuxQuestions.org
Register a domain and help support LQ
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 02-18-2008, 09:25 PM   #1
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Rep: Reputation: 15
SED - Delete line above or below as well as matching line...


Hi,

I am new to sed and though it's powerful, it's really hard on the head!

I have large text files that have two or three lines, then a blank then another two to three lines etc.

I am able to copy out the lines that meet my criteria to a new file with this:

sed -n -e '/matchingword/{=;x;1!p;g;$!N;p;D;}' -e h file > newfile

but I want to delete these lines and copy what’s left to a new file.

I can figure this out for myself in time, but I have many things I need to do for work ASAP and can't stop to learn this right now. I am sure someone out there can roll their eyes and offer up a painfully easy solution :O)


Thanks,
-OG-

Last edited by OldGaf; 02-18-2008 at 09:26 PM. Reason: Tags
 
Old 02-18-2008, 09:45 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
show a sample of that file. and describe what you want and don't want..
 
Old 02-18-2008, 10:25 PM   #3
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
show a sample of that file. and describe what you want and don't want..
Wow..... thanks for the fast responce!

This is a small peice of a file. I would like to get rid of the lines with "Queued" in them as well as the line above (as they are together).

In other files the structure is the same but I will be looking for a string in the first line and will want to get rid of that line as well as the one below. In other files I will be searching a middle line and will want to get rid of the top and bottom as there are three lines together in those files.


server /vol/serverv5/appl_dbau_nfsdumps_instance_test_nydb_sybdump006
44 Missing Files - 0 files on tape - Job 3502673 is Queued on server01

server /vol/serverv1/appl_dbau_nfsdumps_instance_qa_nydb_sybdump0029
13 Missing Files - 0 files on tape - Job 3503005 is Queued on server01

server /vol/serverv2/appl_dbau_nfsdumps_instance_archive_nydb_sybd_01
2 Missing Files - 0 files on tape - Job 3502919 is Queued on server01

server /vol/serverv2/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_05
208 Missing Files - 0 files on tape - Job 3502658 is Active on server01

server /vol/serverv2/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_04
360 Missing Files - 0 files on tape - Job 3502660 is Active on server01

server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_03
381 Missing Files - 0 files on tape - 2 files purged - Job 3502657 is Active on server01

server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_02
505 Missing Files - 0 files on tape - Job 3502656 is Active on server01

server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump019
1 Missing Files - 0 files on tape - 1 files purged

-OG-
 
Old 02-18-2008, 11:12 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Here is a crude start.....I put your example into a file name "2line".

sed '/^$/d' 2line |sed -n 'h;n;H;g;/Que/!p'

Produces this:

server /vol/serverv2/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_05
208 Missing Files - 0 files on tape - Job 3502658 is Active on server01
server /vol/serverv2/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_04
360 Missing Files - 0 files on tape - Job 3502660 is Active on server01
server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_03
381 Missing Files - 0 files on tape - 2 files purged - Job 3502657 is Active on server01
server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_02
505 Missing Files - 0 files on tape - Job 3502656 is Active on server01
server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump019
1 Missing Files - 0 files on tape - 1 files purged

first it deletes the empty lines.
Then it reads a line, puts it in the hold buffer, appends the next line, gets the combo back to pattern space and prints it if it does NOT contain "Que"
 
Old 02-18-2008, 11:27 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Does it have to be sed? With awk, maybe this would do:

awk -vRS="" '$0 !~ /Queued/{ print }'

Makes blank-lines separate records and if a record does not contain 'Queued', it prints it. If keeping the blank line separators matters, just add '-vORS="\n\n"' or something like.

Last edited by slakmagik; 02-18-2008 at 11:29 PM. Reason: cleaned out some cruft
 
Old 02-19-2008, 09:36 PM   #6
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
Here is a crude start.....I put your example into a file name "2line".

sed '/^$/d' 2line |sed -n 'h;n;H;g;/Que/!p'

Produces this:

server /vol/serverv2/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_05
208 Missing Files - 0 files on tape - Job 3502658 is Active on server01
server /vol/serverv2/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_04
360 Missing Files - 0 files on tape - Job 3502660 is Active on server01
server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_03
381 Missing Files - 0 files on tape - 2 files purged - Job 3502657 is Active on server01
server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump016_02
505 Missing Files - 0 files on tape - Job 3502656 is Active on server01
server /vol/serverv1/appl_dbau_nfsdumps_instance_prod_nydb_sybdump019
1 Missing Files - 0 files on tape - 1 files purged

first it deletes the empty lines.
Then it reads a line, puts it in the hold buffer, appends the next line, gets the combo back to pattern space and prints it if it does NOT contain "Que"
I want to keep the spaces for readability....... so I can clip the first part off. THere are other files that I will not want spaces so I can use the whole thing for those..... thanks so much for your help.
 
Old 02-19-2008, 09:37 PM   #7
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by digiot View Post
Does it have to be sed? With awk, maybe this would do:

awk -vRS="" '$0 !~ /Queued/{ print }'

Makes blank-lines separate records and if a record does not contain 'Queued', it prints it. If keeping the blank line separators matters, just add '-vORS="\n\n"' or something like.
awk.... I never thought of that! Great one liner :O)

Neither of these look even close to what I had!
 
Old 06-26-2008, 11:51 PM   #8
aliyesami
LQ Newbie
 
Registered: Jun 2008
Posts: 5

Rep: Reputation: 0
sed question

I sent a question to pixelleny but iam not sure it went cause I got some error msg so I will ask here again and anyone can answer.

hi Pixellany !

I saw your reply to a posting on the forum and I have exactly the same problem slightly different so I thought of first asking to you and then posting it on the forum hope you dont mind .

you put a solution for deleting one line above the pattern line. I want a line above and below the line starting with %CLI-E deleted including the pattern line itself if possible.

so for example I want these the following lines


$CHECKSUM $1$DGA243:[ORACLE.PATRON]FGAG01.DBF;1
%CLI-E-OPENIN, error opening $1$DGA243:[ORACLE.PATRON]FGAG01.DBF;1
-RMS-E-FLK, file currently locked by another user
$CHECKSUM $1$DGA1043:[ORACLE.PATRON]TSTST01.DBF;1
%CLI-E-OPENIN, error opening $1$DGA1043:[ORACLE]TSTST01.DBF
-RMS-E-FLK, file currently locked by another user
$CHECKSUM $1$DGA441:[ORACLE.PATRON]AFARG01.DBF;1
CHECKSUM 12142131


reduced to

%CLI-E-OPENIN, error opening $1$DGA1043:[ORACLE]TSTST01.DBF
%CLI-E-OPENIN, error opening $1$DGA243:[ORACLE.PATRON]FGAG01.DBF;1
$CHECKSUM $1$DGA441:[ORACLE.PATRON]AFARG01.DBF;1
CHECKSUM 12142131

or preferably this

$CHECKSUM $1$DGA441:[ORACLE.PATRON]AFARG01.DBF;1
CHECKSUM 12142131


thanks for your help
Sami
Florida
 
  


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
Delete a line using sed kushalkoolwal Programming 2 09-29-2007 07:25 PM
using sed to delete line to the right khairilthegreat Linux - Newbie 5 07-28-2007 02:10 PM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM
trying to delete a line with sed deoren Linux - General 2 01-03-2005 09:26 PM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM


All times are GMT -5. The time now is 06:43 PM.

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