Operating System - HP-UX
1827603 Members
3007 Online
109966 Solutions
New Discussion

Re: Quick scripting query

 
SOLVED
Go to solution

Quick scripting query

Hi,

I'm in the process of modifying a series of files in one particular directory.

I have attached a copy of the file I need to modify.

Our domain name has changed therefore need to change this in the files. I need to change the line towards the bottom which has the name of the host with it's fully qualified domain name.

This line currently reads:

HOST "dwnps006-mgt.domainx.com" dwnps006-mgt.domainx.com

I need to change this to:

HOST "dwnps006.it.newdomain.net" dwnps006.it.newdomain.net

I need to do this for about 35-40 files. Obviously each host name is different. The name of the host is also in the first line of the file: (after Quotes and before underscore)

DATALIST "dwnps006_system_adhoc"

So first I will need to put hostname in a variable then use this and replace everything after HOST with:

HOST "$hostname.it.newdomain.net" $hostname.it.newdomain.net

I hope this is a straight forward problem. How would I put hostname into a variable so that I can use it to replace old FQDN with new one.

Thanks, any help much appreciated.

Regards,

Mohammed.
Life is what you make it!
14 REPLIES 14
U.SivaKumar_2
Honored Contributor

Re: Quick scripting query

Hi,

hostname=yourhost
HOST "`$hostname`.it.newdomain.net" $hostname.it.newdomain.net


regards,
U.SivaKumar


Innovations are made when conventions are broken
harry d brown jr
Honored Contributor

Re: Quick scripting query

Seriously, for 30-45 files it would be faster AND safer using vi

live free or die
harry
Live Free or Die
U.SivaKumar_2
Honored Contributor

Re: Quick scripting query

Hi,

If domain part is constant.

hostname=yourhostname.it.newdomain.net
HOST "$hostname" $hostname

regards,
U.SivaKumar
Innovations are made when conventions are broken
Tom Jackson
Valued Contributor

Re: Quick scripting query

Hi:

Here's a little script.

awk ' {
FS = "-"
printf "%s.it.newdomain.net\n", $1 } ' fileName.in

Tom
harry d brown jr
Honored Contributor

Re: Quick scripting query


Here's a script:

#!/usr/bin/ksh
#
# PUT your 35-40 file names in /tmp/filelist
# BUT make sure you use the FULL PATH NAME
#
for fname in `cat /tmp/filelist`
do
cat $fname|eval `head -1 $fname|cut -d'"' -f2|cut -d'_' -f1|sed "s/.*/sed \"s_&-
mgt.domainx.com_&.it.newdomain.net_g\"/"`>${fname}TEMP
mv ${fname}TEMP $fname
done


So /tmp/filelist would look like this:

/somepath/somefile1
/somepath/somefile2
...

live free or die
harry
Live Free or Die
Robert Thorneycroft
Valued Contributor

Re: Quick scripting query

You you try:

For i in file1 file2 file3 ...
do
cp $i $i.origcopy
sed "s/dwnps006-mgt.domainx.com/dwnps006.it.newdomain.net/g" $i > $i.tmpcopy
cp $i.tmpcopy $i
done

Once you have done this you can check the files, if everything is messed up replace the .orig files. Once you are sure everything is fine remove the .tmpcopy and .origcopy files.

Kind regards,

Robert Thorneycroft
Robert Thorneycroft
Valued Contributor

Re: Quick scripting query

*Important*
In the above message sed and the next two lines are all part of the same line.

Damn editor needs longer line lengths :(

Regards.
F. X. de Montgolfier
Valued Contributor

Re: Quick scripting query

Hi Mohamed,

an easy way to do so would be:

1. create a file /tmp/filelist containing the list of files you want to update

2. create then run this script:

#! /usr/bin/ksh
for i in `cat /tmp/filelist`
do
sed 's/dwnps006-mgt.domainx.com/dwnps006.it.newdomain/g' $i > /tmp/tempfile
mv tmpfile $i
done

tested on your file
Hope this helps

FiX

Re: Quick scripting query

Thanks for the reply guys,

The hostname dwnps006 is just in that one file. Every other file has a different hostname. This host name is at the top of the file after DATALIST " and before the _

So Ideally I would want to cut the hostname put it in a variable and use that variable at the bottom of the file appending the new FQDN.

By the way... I have to do this for 137 files in dir not 35-40 :)

Regards,

Mohammed.
Life is what you make it!

Re: Quick scripting query

Thanks Harry D Brown... I opted for your approach. However instead of cat directory I did for i in `ls directory`

Thanks.

Regards,

Mohammed.
Life is what you make it!
Ralph Grothe
Honored Contributor

Re: Quick scripting query

Hi Mohammed,

I may miss something here, but this seems to a very simple task
like

perl -i.bak -npe 's/domainx\.com/newdoain\.net/g' filelist


where filelist are the 1000 files you need to change.
Perl's in-place option saves us a backup copy of with the extension .bak in my example of each file, and writes changes to your original files.

If you don't want to specify your filelist use shell globbing or command subtitution from an ls or supply a file with the list
Madness, thy name is system administration
F. X. de Montgolfier
Valued Contributor
Solution

Re: Quick scripting query

Re Hi Mohamed,

I didn't realise the hostname var a parameter...

Will this be what you wanted?

same process as before, put all file names in /tmp/filelist

The shell script becomes:

#! /usr/bin/ksh
for i in `cat /tmp/filelist`
do
host_name=`grep DATALIST test|sed 's/.*DATALIST \"//'|sed 's/_.*//'`

sed 's/"${host_name}"-mgt.domainx.com/"${host_name}".it.newdomain/g' $i > /tmp/tmpfile
mv tmpfile $i
done

Would this be what you're looking for? I know the host_name calculation is not very pretty, but it works ;-)

Cheers,

FiX

Sundar_7
Honored Contributor

Re: Quick scripting query


Something like this


# cd /tothedirwherefilesexists
# find . -type f -print > /tmp/1
# for fil in $(cat /tmp/1)
> do
> sed 's/\([a-z][a-z]*\)-mgt.domainx.com/\1.it.newdomain.net' > ${fil}.new
>done
#

now after testing ${fil}.new files rename them

Sundar.


Learn What to do ,How to do and more importantly When to do ?
Thomas M. Williams_1
Frequent Advisor

Re: Quick scripting query

I have attached a script for you. I believe this will solve your dilemma. It includes two scripts. Just delete what you don't want. The first one somply changes the domain. The second looks at the host name but I do not think you need to worry about the host names since you are only changing the domain, but I stuck it in there anyway.
I Think the Clock is Slow ...