• Post Reply Bookmark Topic Watch Topic
  • New Topic

remove a code block from all files.  RSS feed

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a rookie on unix. So please bear with me.

I have tons of shell files in a folder that has a code block like this , basically a 'if' block.

I want to remove the occurance of this block from all files in that folder.
What unix utility s'd i be using? Would that be sed?
What would my regular expression look like?.





thanks.
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could use sed for this, but the commands might be a bit tricky. (are the "if" blocks always equally long, in terms of number of lines? sed might have a "skip x lines" command that could be of use, if so.)

personally, i might look into scripting the "ed" editor for this, or even scripting "vi"; i would reserve sed for removing single lines, or parts of lines, simply because that kind of sed usage is easiest. if you don't want to look into learning a new utility, however, i'm sure there's some way of bending sed to doing this; the thing's Turing-complete, after all.

look into sed's man page. the commands "d" and "D" with appropriate address ranges seem like good candidates for you; the beginning address maybe should be a regexp matching the "if" line, and the ending address hopefully can be a "+N" constant number of lines, if the blocks in question are always the same length. if they aren't, you may have to use multiline regexps; that could get ugly.

on edit: i have not tested this code, and have no idea if it will actually work - try it at your own risk:
you may have to fiddle with the double-quotes, single-quotes and backslashes to make sure that the regular expression gets through the shell's expansion and globbing correctly so that sed ends up seeing the right regexp.
[ May 18, 2005: Message edited by: M Beck ]
 
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you manage to verify, that a special character is not in any of your scripts, you may use 'tr' to translate linefeeds:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!