1833873 Members
2504 Online
110063 Solutions
New Discussion

Re: Script Help

 
Hunki
Super Advisor

Script Help

I need to change the email domain names on all the files on the system .

Example : abc@xyz.com .. I need to change xyz.com to def.com for all the files on the system.( I need to avoid binaries and log files though )

thanks,
11 REPLIES 11
A. Clay Stephenson
Acclaimed Contributor

Re: Script Help

This would be fairly straightforward using Perl with File::Find and the -T filter or you could use find and then use the file command to identify "text" files (which will include scripts) but what is a log file? ie how do you identify log files. We can guess that they end with .log but that is a guess. Only you will know about any other log files.
Sed can then easily do the editing -- although I would do all of this in Perl.

Learn to clearly define your problem and then the solutions become much more obvious.
If it ain't broke, I can fix that.
Hunki
Super Advisor

Re: Script Help

By log I mean anything that is *log*

thanks,
hunki
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi:

You could use the following pieces. I assume that by "log" files you mean those ending with a ".log" extension, although this is easily changed:

# perl -MFile::Find -le 'find(sub{print $File::Find::name if m/.+\.log/ && -f $_ && -T _}, @ARGV)' /path > filelist

# perl -pi.old -e 's/abc\@xyz.com/def\@xyz.com/g' `cat filelist`

Regards!

...JRF...
Hunki
Super Advisor

Re: Script Help

Hi JRF ,

the command is giving me all log files as output which I dont require. I want to search for all files containing "@abc.com" on the system except for log and binary files and then replace them with "@xyz.com" since the company domain name changed.

thanks,
hunki
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi (again) Hunki:

OK, I had it backwards. To _skip_ files ending in ".log" negate (with '!') the condition. Thus:

# perl -MFile::Find -le 'find(sub{print $File::Find::name if ! m/.+\.log/ && -f $_ && -T _}, @ARGV)' /path > filelist

Regards!

...JRF...
Hunki
Super Advisor

Re: Script Help

this is not giving me the desired out ... can u explain what you are doing thru this perl command.
A. Clay Stephenson
Acclaimed Contributor

Re: Script Help

Once again, you need to better define your problem. It is not clear what "out" is. Does that mean that the file list is incorrect? If so, how? --- OR --- does that mean that the files are not being modified correctly? If so, how?
If it ain't broke, I can fix that.
Hunki
Super Advisor

Re: Script Help

I mean the output that I am getting through the "##perl -MFile::Find -le 'find(sub{print $File::Find::name if ! m/.+\.log/ && -f $_ && -T _}, @ARGV)' /" command. The files that come in the output does not have "@xyz.com" in them.
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi (again):

I provided *two* Perl snippets. The first simply gathers Ascii files while skipping files with ".log" in their name.

I made NO PROVISION to filter any farther than that.

The second Perl script does the actual replacement (if any) while preserving the original files as ".old". It uses (as input) the output generated by the first script.

Between the two, you have the hard work done for you. You can filter (reduce) the output from the first step (e.g. with 'grep') or you can use it directly with the second script if it fits your needs.

I didn't write a full-blown script but offered you re-useable pieces.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Script Help

The second perl script looks fine to me but you should note that there really is no "output" file because the editing is done on the original file. (well, that's almost correct but the net effect is that the original filename contains the updated data). The -i.old Perl option saves a copy of the original input with a .old suffix.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi (again):

I might note that the second Perl script updates "in-place" while making a backup copy of the original file as "*.old".

In the event that no matching patterns are found, the new file and the original would remain identical in content, although the 'mtime' of the current file would be changed to the current date/time since in truth it is a new file.

Regards!

...JRF...