Computing.Net > Forums > Unix > Removing characters from file names

Removing characters from file names

Reply to Message Icon

Original Message
Name: gazzabazza
Date: April 25, 2003 at 05:43:58 Pacific
Subject: Removing characters from file names
OS: unix
CPU/Ram: pentium 3 64mb ram
Comment:

I was wondering if anyone could help me with my scripting problem, i have been asked by a friend to write a script for unix that will take out the first few characters and the last 2 characters,

There is a lot of files and i was wondering if there could be some sort of script that will go through them for me and make the changes.

If anyone could send me a script or show me where to get one or even tips on how to do this i would be very greatful.


Report Offensive Message For Removal

Response Number 1
Name: Frank
Date: April 25, 2003 at 07:19:01 Pacific
Subject: Removing characters from file names
Reply: (edit)

e.g. files with *dat are the files you are interested in:

for FileName in `ls *dat`
do
echo "$FileName" >> newfilename
head -1 $FileName >> newfilename
tail -1 $FileName >> newfilename
e--"
done


Frank


Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: April 25, 2003 at 08:18:14 Pacific
Subject: Removing characters from file names
Reply: (edit)

Hi:

I just cut out the columns I want.I made some assumptions you might want to change:

1) I'm looking at just files passed on command line. If you want to look at everything in the director use 'for i in *'

2) I didn't know how many "few" was so I started cutting at position 3.

3) I didn' overwrite the file if it already exists.

Regards,

Nails

#!/bin/ksh

few=3
l2=2

for i in $*
do
if [ -f $i ]; then
len=$(expr $i : '.*') # length of filename
nf=$(echo $i|cut -c$few-$((len-l2)) )
[ ! -f $nf ] && mv $i $nf
fi
done


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: Removing characters from file names

Comments:

 


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