LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SED - Delete line above or below as well as matching line... (https://www.linuxquestions.org/questions/programming-9/sed-delete-line-above-or-below-as-well-as-matching-line-622123/)

OldGaf 02-18-2008 09:25 PM

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-

ghostdog74 02-18-2008 09:45 PM

show a sample of that file. and describe what you want and don't want..

OldGaf 02-18-2008 10:25 PM

Quote:

Originally Posted by ghostdog74 (Post 3062045)
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-

pixellany 02-18-2008 11:12 PM

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"

slakmagik 02-18-2008 11:27 PM

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.

OldGaf 02-19-2008 09:36 PM

Quote:

Originally Posted by pixellany (Post 3062091)
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.

OldGaf 02-19-2008 09:37 PM

Quote:

Originally Posted by digiot (Post 3062102)
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!

aliyesami 06-26-2008 11:51 PM

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


All times are GMT -5. The time now is 12:57 PM.