The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
deleting lines by number cary530 UNIX for Dummies Questions & Answers 9 03-30-2009 02:09 PM
deleting lines using awk-best way? satnamx Shell Programming and Scripting 3 08-29-2007 07:24 PM
Deleting lines in a file reachsamir Shell Programming and Scripting 2 05-29-2007 11:17 AM
Deleting last 2 lines from the file. videsh77 Shell Programming and Scripting 4 04-06-2007 01:14 PM
deleting lines supercbw High Level Programming 1 05-06-2002 12:53 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-30-2003
ajnabi ajnabi is offline
Registered User
  
 

Join Date: Nov 2001
Location: Atlanta, GA
Posts: 34
deleting lines

I am trying deleting lines from a text file using sed..

sed '/OBJECT="ABC/{N;N;N;d; }'

will do if i have to delete lines starting with Object and next 3 lines
but I was looking for a way to delet lines starting with OBJECT and all the lines till it reaches a blank lines ..or it reaches a line with OBJECT in it ...

i tried all kind of things .. didn't work .

Will appreciate help

thanks in advance ..
  #2 (permalink)  
Old 05-01-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
You have to use a loop...
Code:
#! /usr/bin/ksh
target=OBJECT
sed -n -e "
s/${target}//
t loop
p
b
:loop
s/.*//
n
s/${target}//
t
b loop"
exit 0
  #3 (permalink)  
Old 05-01-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
Not that I can decipher how that works but I invoked it (assuming your script is called theScript and the OP's text file is called theFile) by typing theScript < theFile and it deletes any line with the word OBJECT on it as well as the line immediately following any line with the word OBJECT on it, but nothing else..
  #4 (permalink)  
Old 05-01-2003
ajnabi ajnabi is offline
Registered User
  
 

Join Date: Nov 2001
Location: Atlanta, GA
Posts: 34
deleting lines following pattern

this script deletes line with pattern and again search for pattern and deletes it ..
May be i haven't explained properly...
Once i delete the line with pattern i need to delete all the lines after the pattern till the time it sees a blank line.
I am working on database file ..
which is in this format ..
OBJECT="NAME" ACTION="REDEFINE" "NAME_A"="KAJNABI-XYZ"
DESCRIPTION="FGH"
.
.
.
.
AUTO_RESET="0"
.
.
.
***************BLANK LINE**********
OBJECT="NAME" ACTION="REDEFINE" "NAME_A"="AJNABI-GFT"
DESCRIPTION="GFR"
.
.
.
.
AUTO_RESET="0"
.
.
.

OBJECT="NAME" ACTION="REDEFINE" "NAME_A"="KAJNABI-XYZ"
DESCRIPTION="FGH"
.
.
.
.
AUTO_RESET="0"
.
.
.

WHAT i need to do is delete line with pattern say KAJNABI and delete all the lines after it till it sees the blank line .. and again search for the pattern and does the same again ...
I understand that i will have to use the loop but how exactly I am going to do this .. makes me confused ....

thanks
  #5 (permalink)  
Old 05-01-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
I understand the script in general now..

From what I see, the part where n deletes the next line should delete all lines until the next $target is found, but it doesn't; however, the second instance of s/${target}// should actually be searching for a blank line instead of $target..
  #6 (permalink)  
Old 05-01-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
I tested my script again and it continues to work for me. It starts deleting when it sees a line containing "OBJECT". It keeps on deleting until it sees a second line containing "OBJECT". Then it starts over again.
  #7 (permalink)  
Old 05-01-2003
ajnabi ajnabi is offline
Registered User
  
 

Join Date: Nov 2001
Location: Atlanta, GA
Posts: 34
code:--------------------------------------------------------------------------------#! /usr/bin/ksh
target=OBJECT
sed -n -e "
s/${target}//
t loop
p
b
:loop
s/.*//
n
s/^$// # till the blank line
t
b loop"
exit 0

did the job ...
Thanks perderabo ...
but for some reason i was running script the way it was .. was just deleting one line containing the pattern...Just curious why it ws doing that .. i was trying running this script on database file .. its in the format the way i mentioned in my post...and say my pattern is KAJNABI ...


This is the best forum i have seen ....

Thanks guys
Closed Thread

Bookmarks

Tags
sed delete line

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0