Computing.Net > Forums > Unix > sed deletestext between delimiter

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sed deletestext between delimiter

Reply to Message Icon

Name: snomys
Date: July 27, 2006 at 04:30:29 Pacific
OS: win nt
CPU/Ram: pentium 4/256
Product: 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;}'




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: July 27, 2006 at 09:08:01 Pacific
Reply:

you almost got it:

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


0

Response Number 2
Name: snomys
Date: July 27, 2006 at 10:00:25 Pacific
Reply:

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!




0

Response Number 3
Name: lchi2000g
Date: July 27, 2006 at 10:20:40 Pacific
Reply:

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


0

Response Number 4
Name: nails
Date: July 27, 2006 at 12:16:25 Pacific
Reply:

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



0

Response Number 5
Name: ghostdog
Date: July 27, 2006 at 21:09:04 Pacific
Reply:

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



0

Related Posts

See More



Response Number 6
Name: snomys
Date: July 28, 2006 at 02:31:06 Pacific
Reply:

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??


0

Response Number 7
Name: lchi2000g
Date: July 28, 2006 at 08:14:16 Pacific
Reply:

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


0

Response Number 8
Name: lchi2000g
Date: July 28, 2006 at 09:02:53 Pacific
Reply:

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


0

Response Number 9
Name: daelomin
Date: August 2, 2006 at 09:36:37 Pacific
Reply:

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...


0

Response Number 10
Name: James Boothe
Date: August 7, 2006 at 14:43:43 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: sed deletestext between delimiter

sed with delimiter, bourne www.computing.net/answers/unix/sed-with-delimiter-bourne/4871.html

sed dropping last line of file www.computing.net/answers/unix/sed-dropping-last-line-of-file/5125.html

parameters in sed www.computing.net/answers/unix/parameters-in-sed/6942.html