?
Solved

include lines in files

Posted on 2011-10-24
12
Medium Priority
?
279 Views
Last Modified: 2012-05-12
Hello,

I am requiring some help with a way to to take several directories located in
c:\dhcp\

There are several directories under c:\dhcp\ like
c:\dhcp\r1
c:\dhcp\r2
c:\dhcp\WO

etc....

In each directory for example
c:\dhcp\r2

There is a file named dhcpd.conf

What I am interested in...is the contents of dhcpd.conf looking for any include lines like "/etc/dhcpd/...";

For example in c:\dhcp\r2\dhcpd.conf  there are several files in there with varoious names.

The dhcpd.conf file has various stuff in the file, then we hit a line or lines that looks like

#Include R10 Tongass Craig                        
include "/etc/dhcpd.conf.craig.in";    

#Include R10 Tongass Hoonah                        
include "/etc/dhcpd.conf.hoonah.in";

If the above matches, I need to append all the includes from all the flies its calling into the master file named dhcpd.conf but rename it to c:\dhcp\r2\dhcpd.conf-r2

As the /etc/.... does not exist where the includes are, perhaps if the script can ignor /etc/  part??

Then move on to the next directory and so on.

Can this perl scripted to where I can run it something like...

c:\dhcp\append-dhcpd-confs.pl

I can use cygwin as well if thats easier

Thanks in advance !

0
Comment
Question by:richsark
  • 6
  • 6
12 Comments
 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 37022259
Using shell script under cygwin:

#!/usr/bin/sh
for mathcingFiles in `find ./ -type f -name dhcpd.conf -exec egrep -l "include .*dhcpd.conf" {} \;`
do
	echo Taking the backup
	echo cp -i $mathcingFiles $mathcingFiles".Original"
	sed "s/\(include \"\/etc\/dhcpd.conf\)\(.*\)/\1/" $mathcingFiles > "$mathcingFiles"-r2
	echo created the file "$mathcingFiles"-r2
	echo mv -i "$mathcingFiles"-r2 $mathcingFiles
done

Open in new window


In this script I have written echo for moving(renaming the files)
You can replace
echo mv -i "$mathcingFiles"-r2 $mathcingFiles

Open in new window

with
mv -i "$mathcingFiles"-r2 $mathcingFiles

Open in new window

or with:
mv "$mathcingFiles"-r2 $mathcingFiles

Open in new window




I have the following commands available under cygwin:
1. find
2. egrep
3. echo
4. cp
5. sed
6. mv
0
 
LVL 1

Author Comment

by:richsark
ID: 37022306
Great !

So.... the directory paths that your script understands are

c:\dhcp\r1
c:\dhcp\r2
c:\dhcp\r3
c:\dhcp\r4
c:\dhcp\r5
c:\dhcp\rw0
and so on......

will work with your above script to understand where the dhcpd.conf came from. Not only being in r2.

Just making sure.

Thank you in advance
0
 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 37023809
>will work with your above script to understand where the dhcpd.conf came from. Not only being in r2.

Yes.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:richsark
ID: 37026735
Great !! I look forward in your reply !

Thank you kindly
0
 
LVL 1

Author Comment

by:richsark
ID: 37026846
Hi,

Just to let you know, I ran your script as is. Looks good,

But, can we remove/delete all the

include "/etc/dhcpd.conf";

Statements when the new file is created? since there all combined now, no need to call it in the new file.

Thanks
0
 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 37029253
Yes
Requirement:
1. Replace all the files with the file name as dhcpd.conf and it contains the following:
include "/etc/dhcpd.conf";
Here goes the script this requirement:
#!/usr/bin/sh
for mathcingFiles in `find ./ -type f -name dhcpd.conf -exec egrep -l "include \"\/etc\/dhcpd.conf\";" {} \;`
do
	echo Taking the backup
	echo cp -i $mathcingFiles $mathcingFiles".fileContainingInlude"
	cp -i $mathcingFiles $mathcingFiles".fileContainingInlude"
	sed "s/\(include \"\/etc\/dhcpd.conf\"\;\)//" $mathcingFiles > "$mathcingFiles"temporary
	echo created the file "$mathcingFiles"temporary
	mv "$mathcingFiles"temporary $mathcingFiles
	echo Updated the file $mathcingFiles
done

Open in new window



2. Replace all the files with the file name as dhcpd.conf and it contains the following line:
include "/etc/dhcpd.confFOLLOWED by anything:......
Here goes the script this requirement2:
#!/usr/bin/sh
for mathcingFiles in `find ./ -type f -name dhcpd.conf -exec egrep -l "include \"\/etc\/dhcpd.conf" {} \;`
do
	echo Taking the backup
	echo cp -i $mathcingFiles $mathcingFiles".fileContainingInlude"
	cp -i $mathcingFiles $mathcingFiles".fileContainingInlude"
	sed "s/\(include \"\/etc\/dhcpd.conf.*\)//" $mathcingFiles > "$mathcingFiles"temporary
	echo created the file "$mathcingFiles"temporary
	mv "$mathcingFiles"temporary $mathcingFiles
	echo Updated the file $mathcingFiles
done

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 37035766
Hi,

Still have an issue with your new script. It still does not propergate down all the folders.

Looks like 5 of the 10.

Do you need anything else to help figure this out?
0
 
LVL 10

Accepted Solution

by:
Murugesan Nagarajan earned 2000 total points
ID: 37042980
cd /cygdrive/c/dhcp
echo contents of files containing include "/etc/dhcpd.conf";
contents of files containing include "/etc/dhcpd.conf";


$ find ./ -type f -name dhcpd.conf -exec egrep -l "include \"\/etc\/dhcpd.conf\";" {} \;
./dhcpd.conf
./r1/dhcpd.conf


$ for matchinfFiles in `find ./ -type f -name dhcpd.conf -exec egrep -l "include \"\/etc\/dhcpd.conf\";" {} \;`
do
       echo content of matching file $matchinfFiles
       echo ----------------------------
       cat $matchinfFiles
       echo ----------------------------
done

Open in new window



OUTPUT above the for loop:
content of matching file ./dhcpd.conf
----------------------------
#Include R10 Tongass Craig
include "/etc/dhcpd.conf";
----------------------------
content of matching file ./r1/dhcpd.conf
----------------------------
#Include R10 Tongass Craig
include "/etc/dhcpd.conf";
----------------------------

Open in new window


Output using result.sh
$ result.sh
Taking the backup
cp -i ./dhcpd.conf ./dhcpd.conf.fileContainingInlude
created the file ./dhcpd.conftemporary
Updated the file ./dhcpd.conf
Taking the backup
cp -i ./r1/dhcpd.conf ./r1/dhcpd.conf.fileContainingInlude
created the file ./r1/dhcpd.conftemporary
Updated the file ./r1/dhcpd.conf

$ for matchinfFiles in `find ./ -type f -name dhcpd.conf -exec egrep -l "include \"\/etc\/dhcpd.conf\";" {} \;`
do
         echo content of matching file $matchinfFiles
         echo ----------------------------
         cat $matchinfFiles
         echo ----------------------------
done

Open in new window

Not output obtaines in the for loop mentioned above.
$ cat ./dhcpd.conf
#Include R10 Tongass Craig


$ cat ./r1/dhcpd.conf
#Include R10 Tongass Craig

Open in new window


Can you explain the following in detail.
	Still have an issue with your new script. It still does not propergate down all the folders.
	Looks like 5 of the 10.

Open in new window

0
 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 37042994
The line mentioned above
          Not output obtaines in the for loop mentioned above.
is
          No output obtained in the for loop mentioned above.
0
 
LVL 1

Author Comment

by:richsark
ID: 37048486
HI,

I am not understanding your last comment about:


The line mentioned above
          Not output obtaines in the for loop mentioned above.
is
          No output obtained in the for loop mentioned above.
0
 
LVL 10

Expert Comment

by:Murugesan Nagarajan
ID: 37048984
In the comment posted on
10/28/11 10:56 AM, ID: 37042980

I have mentioned the following statement:
         Not output obtaines in the for loop mentioned above.
which needs to be
         No output obtained in the for loop mentioned above.
0
 
LVL 1

Author Comment

by:richsark
ID: 37051513
Ok. I sorta understand, Let me run it again and revert.

Thanks

Rich
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question