include lines in files

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 !

LVL 1
richsarkAsked:
Who is Participating?
 
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
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
 
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
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
 
richsarkAuthor Commented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
>will work with your above script to understand where the dhcpd.conf came from. Not only being in r2.

Yes.
0
 
richsarkAuthor Commented:
Great !! I look forward in your reply !

Thank you kindly
0
 
richsarkAuthor Commented:
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
 
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
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
 
richsarkAuthor Commented:
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
 
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
The line mentioned above
          Not output obtaines in the for loop mentioned above.
is
          No output obtained in the for loop mentioned above.
0
 
richsarkAuthor Commented:
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
 
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
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
 
richsarkAuthor Commented:
Ok. I sorta understand, Let me run it again and revert.

Thanks

Rich
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.