Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i have a file where i need to remove text between the delimiters.
say...
delimiter1 = start_block
delmiter2 = no_blockso...
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;}'

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_blockas 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!

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
===>2Luke Chi

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

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

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_blockstart_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_blockstart_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_blockblank lines between occurences of no_block and start_block are removed??

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

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_removeLuke Chi

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

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

![]() |
![]() |
![]() |

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