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