Problem renaming files on Solaris 10 server

Good day all.

I'm trying to rename some files in my home directory with some bizarre results. Basically I need to change the IP address in the filename to the hostname which I ggrep from within the file:

 
-rw-r--r--   1 bh694n   nrc         5095 May  2 20:03 alarms_999.189.161.146.log
-rw-r--r--   1 bh694n   nrc         4450 May  2 20:03 alarms_999.189.161.5.log
-rw-r--r--   1 bh694n   nrc         4398 May  2 20:03 alarms_999.189.175.143.log
-rw-r--r--   1 bh694n   nrc         4450 May  2 20:03 alarms_999.189.175.20.log
-rw-r--r--   1 bh694n   nrc         4397 May  2 20:03 alarms_999.190.152.231.log
-rw-r--r--   1 bh694n   nrc         4400 May  2 20:03 alarms_999.190.152.76.log
-rw-r--r--   1 bh694n   nrc         4397 May  2 20:03 alarms_999.190.161.232.log
-rw-r--r--   1 bh694n   nrc         5227 May  2 20:03 alarms_999.191.152.220.log
-rw-r--r--   1 bh694n   nrc         4429 May  2 20:03 alarms_999.191.152.77.log
-rw-r--r--   1 bh694n   nrc         4398 May  2 20:03 alarms_999.191.184.231.log
-rw-r--r--   1 bh694n   nrc         4419 May  2 20:03 alarms_999.191.184.52.log
-rw-r--r--   1 bh694n   nrc         5512 May  2 20:03 alarms_999.192.104.46.log
-rw-r--r--   1 bh694n   nrc         4398 May  2 20:03 alarms_999.192.106.46.log
-rw-r--r--   1 bh694n   nrc         5508 May  2 20:03 alarms_999.193.171.214.log
-rw-r--r--   1 bh694n   nrc         5453 May  2 20:03 alarms_999.193.171.53.log
-rw-r--r--   1 bh694n   nrc         4398 May  2 20:03 alarms_999.193.177.220.log
-rw-r--r--   1 bh694n   nrc         4397 May  2 20:03 alarms_999.193.177.52.log
 

The command string I used:

for i in `ls alarm*.log`;do HOST=`/usr/sfw/bin/ggrep -A 1 sysidconf $i|tail -1`;mv $i alarms_${HOST}.log;done

What I get is this:

bh694n$ ls -ltr        
total 180
.logr--r--   1 bh694n   nrc         5095 May  2 20:03 alarms_wa1dc02scg
.logr--r--   1 bh694n   nrc         4450 May  2 20:03 alarms_wa1dc01scg
.logr--r--   1 bh694n   nrc         4398 May  2 20:03 alarms_wy1pa02scg
.logr--r--   1 bh694n   nrc         4450 May  2 20:03 alarms_wy1pa01scg
.logr--r--   1 bh694n   nrc         4397 May  2 20:03 alarms_al1ga02scg
.logr--r--   1 bh694n   nrc         4400 May  2 20:03 alarms_al1ga01scg
.logr--r--   1 bh694n   nrc         4397 May  2 20:03 alarms_na1tn02scg
.logr--r--   1 bh694n   nrc         5227 May  2 20:03 alarms_ka1mo02scg
.logr--r--   1 bh694n   nrc         4429 May  2 20:03 alarms_ka1mo01scg
.logr--r--   1 bh694n   nrc         4398 May  2 20:03 alarms_sn1tx02scg
.logr--r--   1 bh694n   nrc         4419 May  2 20:03 alarms_sn1tx01scg
.logr--r--   1 bh694n   nrc         5512 May  2 20:03 alarms_cl1oh02scg
.logr--r--   1 bh694n   nrc         4398 May  2 20:03 alarms_ch1il02scg
.logr--r--   1 bh694n   nrc         5508 May  2 20:03 alarms_so1ca02scg
.logr--r--   1 bh694n   nrc         5453 May  2 20:03 alarms_so1ca01scg
.logr--r--   1 bh694n   nrc         4398 May  2 20:03 alarms_oa1ca02scg
.logr--r--   1 bh694n   nrc         4397 May  2 20:03 alarms_oa1ca01scg

:eek:

Where did I err?

Regards,
Bjoern

The output from ggrep -A 1 sysidconf $i ends with a DOS <carriage-return><newline> character pair as a line terminator instead of the UNIX <newline> character line terminator.

Are these log files from a DOS system?

From where you are now, you should be able to recover using ksh or /usr/xpg4/bin/sh (not /bin/sh ) to run the for loop:

for i in alarms*.log
do	mv "$i" "${i%?.log}.log"
done

and if you want to run your original script again and avoid this step, change it from:

for i in `ls alarm*.log`;do HOST=`/usr/sfw/bin/ggrep -A 1 sysidconf $i|tail -1`;mv $i alarms_${HOST}.log;done

to:

for i in alarm*.log;do HOST=`/usr/sfw/bin/ggrep -A 1 sysidconf "$i"|tail -1|tr -d '\r'`;mv "$i" "alarms_${HOST}.log";done

Thanks Don, your solution is perfect!

The log files are pure unix. I'm familiar with the Linux grep '-A and -B' to find the lines leading and trailing the context of the matching lines. As far as I know the Solaris grep does not have that function, so I used ggrep.

Regards,
Bjoern

---------- Post updated at 01:44 PM ---------- Previous update was at 01:03 PM ----------

Ugh! Do you know VBscript? I'm attempting to run this renaming command from a VBscript in SecureCRT and get a compilation error:

Microsoft VBScript error
Error: Invalid character

---------- Post updated at 02:30 PM ---------- Previous update was at 01:44 PM ----------

No worries, had to remove all the double quotes, it works just fine now.

Thanks again Don.

Bjoern