Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

sed deletestext between delimiter

Original Message
Name: snomys
Date: July 27, 2006 at 04:30:29 Pacific
Subject: sed deletestext between delimiter
OS: win nt
CPU/Ram: pentium 4/256
Model/Manufacturer: IBM
Comment:
i have a file where i need to remove text between the delimiters.

say...

delimiter1 = start_block
delmiter2 = no_block

so...

start_block

data....

no_block

i am new to using sed, but i think its something like this, with some gump dumped into the curly brackets at the end of the expression. there are also multiple instances of this in a file, i want to delete these globally.

sed -e '/start_block/,/no_block/{p;}'



Report Offensive Message For Removal


Response Number 1
Name: nails
Date: July 27, 2006 at 09:08:01 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
you almost got it:

sed '/start_block/,/no_block/d' myfile


Report Offensive Follow Up For Removal

Response Number 2
Name: snomys
Date: July 27, 2006 at 10:00:25 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
actually i tried your posting before you posted, it doesnt work.

heres the data i want to clean up:

start_block
aml_daily al_crossref al_general itm
--------- ---

0 record(s) selected.
no_block


start_block
aml_monthly al_crossref al_general itm
--------- ----
- 43060. 43060.
- 43060. 43060.
- 43060. 43060.
3 record(s) selected.
no_block

as you can see ive put in the delimiters as specified in my first posting.

when i use your idea, it just removes the space between the first appearnce of no_block to the next start_block - all blank lines??

im sure the fact that the first thing to appear in the file is a start_block delimiter is throwing this off from working.

anyone or other ideas would be most welcome, its been driving me nuts all day!




Report Offensive Follow Up For Removal

Response Number 3
Name: lchi2000g
Date: July 27, 2006 at 10:20:40 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
nails's command is correct.

/home/oracle/luke/tmp$ cat file
===>1
start_block
aml_daily al_crossref al_general itm
--------- ---

0 record(s) selected.
no_block

===>3

start_block
aml_monthly al_crossref al_general itm
--------- ----
- 43060. 43060.
- 43060. 43060.
- 43060. 43060.
3 record(s) selected.
no_block

===>2

/home/oracle/luke/tmp$ sed '/start_block/,/no_block/d' file
===>1

===>3


===>2

Luke Chi


Report Offensive Follow Up For Removal

Response Number 4
Name: nails
Date: July 27, 2006 at 12:16:25 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
I'm with Luke, I don't see your problem. According to the data file you posted, all that should print is blank lines. Perhaps I'm missing something. Also, I think this awk script emulates what you are trying to do:

awk ' BEGIN { skip=0 }

/start_block/ { skip=1; continue }

/no_block/ { skip=0; continue }

skip == 0 { print $0 }
' myfile



Report Offensive Follow Up For Removal

Response Number 5
Name: ghostdog
Date: July 27, 2006 at 21:09:04 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
The "algorithm" for such task is typically like this

flag = 1
for lines in the_file:
... if lines is equal "start_block"
...............print lines
...............flag = 1
... if flag is 0:
...............print lines
... if lines is equal "no_block":
...............print lines
...............flag = 0



Report Offensive Follow Up For Removal


Response Number 6
Name: snomys
Date: July 28, 2006 at 02:31:06 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
OK, let me clarify this one more time, im sure Lukes command is fantastic, but as I say its not doing what i require:

My file looks like this:

start_block
aml_daily al_crossref al_general itm
--------- ---

0 record(s) selected.
no_block

===>3

start_block
aml_monthly al_crossref al_general itm
--------- ----
- 43060. 43060.
- 43060. 43060.
- 43060. 43060.
3 record(s) selected.
end_block

start_block
aml_annual al_crossref al_general itm
--------- ---

0 record(s) selected.
no_block


i want to remove all text between the start_block and no_block delimiters ive set,
because i want to retain the data between the start_block and end_block.

as i said earlier, after running the delete command above this is what happens:

start_block
aml_daily al_crossref al_general itm
--------- ---

0 record(s) selected.
no_block
start_block
aml_monthly al_crossref al_general itm
--------- ----
- 43060. 43060.
- 43060. 43060.
- 43060. 43060.
3 record(s) selected.
end_block

start_block
aml_annual al_crossref al_general itm
--------- ---

0 record(s) selected.
no_block
start_block
aml_testr al_crossref al_general itm
--------- ---

0 record(s) selected.
no_block

blank lines between occurences of no_block and start_block are removed??


Report Offensive Follow Up For Removal

Response Number 7
Name: lchi2000g
Date: July 28, 2006 at 08:14:16 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
1. nails offered the command, not me. "im sure Lukes command is fantastic" is not correct.

2. sed '/start_block/,/no_block/x' file will create the wrong result you created and described.

3. make sure using:
sed '/start_block/,/no_block/d'
not:
sed '/start_block/,/no_block/x'


Luke Chi


Report Offensive Follow Up For Removal

Response Number 8
Name: lchi2000g
Date: July 28, 2006 at 09:02:53 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
The delimiters you use make things complicated. You shouldn't have used start_block for both kinds of the blocks - want to keep block and wnat to remove block.

For example, the following delimiter names will make things much easiler:

start_keep, end_keep
start_remove, end_remove

Luke Chi


Report Offensive Follow Up For Removal

Response Number 9
Name: daelomin
Date: August 2, 2006 at 09:36:37 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
what he wants is conditionnal testing of which "end" delimiter you have.

If the end delimiter is "no block" you trash all data in between. If it's "end_block" you keep all data in between the two delimiters.

It's trickier than I know since you need to test all lines past the "start_block" command...till one of the two end delimiters is found...


Report Offensive Follow Up For Removal

Response Number 10
Name: James Boothe
Date: August 7, 2006 at 14:43:43 Pacific
Subject: sed deletestext between delimiter
Reply: (edit)
This awk solution will delete each block of lines that starts with start_block and terminates with no_block rather than end_block.

The logic is that it will print each line as it comes to it UNTIL it begins a block of lines that starts with start_block.  At that point, it will buffer the lines until the block terminates with either no_block or end_block, at which point it will either discard or print the held lines, and so on.

awk '{
if ($0=="start_block")
   flag=1
if (flag!=1)
  {print
   next}
datablock=datablock newline $0
newline="\n"
if ($0=="no_block")
  {datablock=""
   flag=0
   newline=""}
if ($0=="end_block")
  {print datablock
   datablock=""
   flag=0
   newline=""}
}' myfile


I also coded this as a sed one-liner, which uses the very same approach (and I think it took me longer to code this one-liner).

sed -e '/start_block/!b' -e :a -e '$!N' -e /no_block/d -e /end_block/b -e '$!ba' myfile


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed deletestext between delimiter

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC