Click to See Complete Forum and Search --> : Automatic multiple line deletions?
xs411
06-14-2001, 07:58 PM
I'm trying to write a script to del multiple lines within a file. The structure looks like this:
[some-server-name]
path = /path/to/my/server/directory
public = no
writable = yes
read only = no
The number of lines never changes and the only change in content is the name and the path. So I could use the name to find the first line and then delete the next 5 lines or something. Any ideas on how I would do this? I was thinking I could use sed maybe?
Thanks,
XS411
iDxMan
06-14-2001, 11:27 PM
Here's an untested 1 second guess.. It removes all sections starting with [ and the 5 lines below it..
#!/usr/bin/perl
$ln = 0;
while(<> )
{
next if $. < $ln;
if ((/^\[/) && (! /^\[global\]/i))
{
$ln = $. + 5;
next;
}
print;
}
Note: I added the `global` skip in there since the format looked like a samba config...
Oh yah.. run it like :
./prog text_file > out
-r
[ 14 June 2001: Message edited by: iDxMan ]
xs411
06-18-2001, 12:30 PM
Well, you were right about the samba config. The code looks good, but how do I specify that I only want to get rid of
[this_share]
and this
and this
and this
and this
I mean basically I have more than just global in there and I need to be able to pick out only one share at a time.
Thanks,
XS411
iDxMan
06-23-2001, 07:46 PM
Originally posted by xs411:
<STRONG>Well, you were right about the samba config. The code looks good, but how do I specify that I only want to get rid of
[this_share]
and this
and this
and this
and this
I mean basically I have more than just global in there and I need to be able to pick out only one share at a time.
Thanks,
XS411</STRONG>
No prob.. This should do the trick.. Currently you just run like ./prog smb.conf this_share >new.conf
Come to think of it I might keep this around for use at work.. :D
#!/usr/bin/perl
$section = pop(@ARGV);
$skip = '^\[global\]|^\[.+\]';
while(<> )
{
$flag = 0 if (/$skip/);
next if ($flag);
if (/^\[$section\]/)
{
$flag = 1;
next;
}
print;
}