Delete Blank Lines Between DHCP Host Blocks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete Blank Lines Between DHCP Host Blocks
# 1  
Old 11-15-2008
Bug Delete Blank Lines Between DHCP Host Blocks

Hi All,

I have a dhcpd.conf file that gets static hosts added and removed via a shell script. After sometime, there becomes huge gaps of space ( blank lines ) between each host block. I tried a couple of sed one-liners; but, I can't seem to get the output I'm looking for. Also, I would like to delete the blank lines between the host blocks interactively instead of piping to a file then doing a move back to the original file. Below is the output I'm looking for:

old dhcpd.conf:

host example1 {
hardware ethernet 00:00:00:00:00:01;
fixed-address 192.168.135.2;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}


host example2 {
hardware ethernet 00:00:00:00:00:02;
fixed-address 192.168.135.3;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}

host example3 {
hardware ethernet 00:00:00:00:00:03;
fixed-address 192.168.135.4;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}



new dhcpd.conf file:

host example1 {
hardware ethernet 00:00:00:00:00:01;
fixed-address 192.168.135.2;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}
host example2 {
hardware ethernet 00:00:00:00:00:02;
fixed-address 192.168.135.3;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}
host example3 {
hardware ethernet 00:00:00:00:00:03;
fixed-address 192.168.135.4;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}


Any help would be greatly appreciated. Thanks in advance!
# 2  
Old 11-15-2008
Hammer & Screwdriver What about the following one-liner?

> cat file63
host example1 {
hardware ethernet 00:00:00:00:00:01;
fixed-address 192.168.135.2;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}


host example2 {
hardware ethernet 00:00:00:00:00:02;
fixed-address 192.168.135.3;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}

host example3 {
hardware ethernet 00:00:00:00:00:03;
fixed-address 192.168.135.4;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}




> cat file63 | tr -s "\n" >file63x ; cp file63x file63 ; rm file63x
> cat file63
host example1 {
hardware ethernet 00:00:00:00:00:01;
fixed-address 192.168.135.2;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}
host example2 {
hardware ethernet 00:00:00:00:00:02;
fixed-address 192.168.135.3;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}
host example3 {
hardware ethernet 00:00:00:00:00:03;
fixed-address 192.168.135.4;
option subnet-mask 255.255.255.0;
option routers 192.168.135.1;
next-server 192.168.135.210;
filename "linux-install/pxelinux.0";
}
# 3  
Old 11-15-2008
If you want to skip the blanc lines within the blocks:

Code:
awk '$0~/{/{f=0} $0~/}/{f=1} f&&!NF{next}1' file > file.tmp && mv file.tmp file

# 4  
Old 11-15-2008
Both solutions remove the blank lines; however, Joeyg's example removes all blank lines. I just need to match the dhcp host blocks then reove blank lines.

Franklin52's example removes all the blank lines; but, the example first needs to read in the file, copy the output to a new file, then move the new file back to the original.

Any SED gurus have an interactive example? Meaning make the changes take place without piping the output to a new file.

I can copy the host blocks to holding buffer and delete; but, I can not copy them back correctly from the holding buffer. Here is what I got so far:


sed -e '/host/,/}/{h; d;}' -e '/$/{G;}' dhcpd.conf



The above command appends the "}" to the end of the file, not the whole host block. Also, it is not doing it interactively. I tried adding the -i option. It didn't work.
# 5  
Old 11-15-2008
If Perl is acceptable:

Code:
perl -i~ -ne'print if /{/../}/' dhcpd.conf

You may try the same with sed but you need an implementation that supports the i option:

Code:
sed -i~ -n '/{/,/}/p' dhcpd.conf

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

3. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

4. Shell Programming and Scripting

Delete blank lines from a file

Hi, I want to use diff to compare two files in a Perl file. But one of the files has some blank lines at the end. So I want to delete the blank lines from the file firstly and then use diff to compare them. But I dont know how to delete the blank lines from the files. Meanwhile, the system is... (5 Replies)
Discussion started by: Damon_Qu
5 Replies

5. Shell Programming and Scripting

Why cant i delete blank lines?

I have a sed pipeline: myVar=$(cat $FILE | sed -n '/regex/,/regex/{/regex/d;p}' | sed -n '/regex/!p' | sed -e s/*:// | sed /regex/,+8d \ ) sed '/^$/d' sed '/./!d' And i've tried to add that in a different order rather then just on the end..Why isnt it deleting all the blank... (2 Replies)
Discussion started by: omgsomuchppl
2 Replies

6. UNIX for Dummies Questions & Answers

delete traling blank lines only

Hi, I have blank line in the file, I just want to remove trailing blank lines. There are some blank lines between the lines, i don't want remove those. Just want to delete blank lines at the end. I used this command sed '/^$/d' infile > outfile, but it is not removing anything. I think... (1 Reply)
Discussion started by: visu
1 Replies

7. Shell Programming and Scripting

Delete blocks of lines from text file

Hello, Hello Firends, I have file like below. I want to remove selected blocks say abc,pqr,lst. how can i remove those blocks from file. zone abc { blah blah blah } zone xyz { blah blah blah } zone pqr { blah blah blah } (4 Replies)
Discussion started by: nrbhole
4 Replies

8. UNIX for Dummies Questions & Answers

delete blank lines from a file

can anyone show me how to delete blank lines from a file. thanks in advance (2 Replies)
Discussion started by: sachin.gangadha
2 Replies

9. Shell Programming and Scripting

delete blank lines with sed

I have a text file with blank lines fullfilled with spaces and others only containing the "Enter" caracter, the \012. I would like to eliminate all them with the sed command. Is it possible? making: sed '/^$/d' <file should delete the blank lines but doesn't work for the lines that only... (2 Replies)
Discussion started by: tmxps
2 Replies

10. UNIX for Dummies Questions & Answers

delete blank lines or lines with spaces only

hi there i'm trying to delete blank lines and or lines with spaces only from a series of files in an directory. to do so, i'm using this: for files in `ls /users/myname/pesop* 2>/dev/null` do grep -v ^$ $files > newfile mv newfile $files done now, this works great for blank lines but... (3 Replies)
Discussion started by: vascobrito
3 Replies
Login or Register to Ask a Question