Computing.Net > Forums > Unix > Shell script to read a file and pro

Shell script to read a file and pro

Reply to Message Icon

Original Message
Name: kishan24
Date: December 4, 2002 at 11:20:55 Pacific
Subject: Shell script to read a file and pro
OS: Windows 2000
CPU/Ram: 128MB
Comment:

Hi,
I am trying to read a file which has two fields i.e tab seperated.
Each line has an OldImage Name and a NewImage Name.
Can anyone help me to write a shell script which reads the file and changes the image name to the newer one.

No Need to check if image exists or not and the permissions of the file.

Thanks in advance.



Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: December 10, 2002 at 08:52:26 Pacific
Reply: (edit)

So this file contains file names, and you want each old filename renamed to each new filename? If so, the following script will do that.

Note that TAB needs to be an actual tab character, and not the word TAB.

No checks are done to see if the move will be or was successful. If you do not want to see error messages, you can redirect the mv stderr to /dev/null. If new name already exists, the mv command will blow that file away, assuming permissions allow. Delete the print command if you do not want those message lines.

#!/bin/sh
cat myfile |
while IFS="TAB" read word1 word2 other
do
print "Renaming $word1 to $word2 ..."
mv $word1 $word2
done
exit 0


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Shell script to read a file and pro

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge