cancel
Showing results for 
Search instead for 
Did you mean: 

Change filename with awk

 
SOLVED
Go to solution
John Carver
Frequent Advisor

Change filename with awk

I need to rename a series of files that contain spaces in their names.

Batch(6)_9_25_2009 4-22-25 PM.pdf

I would like to replace the spaces with the underscore character.

Batch(6)_9_25_2009_4-22-25_PM.pdf

Or possibly remove all underscore characters and spaces.

Batch(6)925200942225PM.pdf

I know awk can handle this, but unfortunatly the specific command syntax is a little outside my awk experience.
8 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Change filename with awk

John:

Variously, 'sed' or 'awk' or Perl will do:

# X="Batch(6)_9_25_2009 4-22-25 PM.pdf"

# echo ${X}|sed -e 's/ //g'

# echo ${X}|awk '{gsub(/ /,//,$0);print}'

In the 'awk' snippet, if you wanted to confine your substitution to a particular field then you could use the appropriate field number ( other than $0).

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor
Solution

Re: Change filename with awk

Hi (again):

Oops; I bungled the 'awk'. Too, you may not want to collapse spaces, but rather to substitute a space with an underscore.

# echo ${X}|sed -e 's/ /_/g'

Awk is a bit odd in the way it defines the arguments to 'gsub'. Use this:

# echo ${X}|awk '{gsub(/ /,"_",$0);print}'

Regards!

...JRF...


Steven E. Protter
Exalted Contributor

Re: Change filename with awk

Shalom,

Perl snippet.


$commandline = "cp ${filetoback} ${filetoback}.bck";
system("${commandline}");
$commandline = "sed s/investmenttool.com/isnamerica.com/g ${filetoback} > $filetoback.bck";
print ("Converting: ${convertiasbackupdir}${filetoback}\n");
print LOGF ("Converting: ${filetoback}\n");
system("${commandline}");
$commandline = "cp ${filetoback}.bck ${filetoback}";
system("${commandline}");


Basically uses the sed to change the contents of a file.

To merely change the name a mv command can do the job.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Goran Koruga
Honored Contributor

Re: Change filename with awk

Hello.

Much better to use mmv or prename (might be called something else in your distribution - it's a handy Perl script).

Regards,
Goran

Re: Change filename with awk

Hello,

Almost the same as JRF's but with no assumption on the number of subsequent spaces :

ls *\ * | awk '{ name = $0 ; gsub ( / */, "_" ) ; printf ( "mv \"%s\" %s\n", name, $0 ) }' | ksh

Be careful there are two spaces in / */

Cheers,

Jean-Philippe
John Carver
Frequent Advisor

Re: Change filename with awk

Almost there. I've incorporated sed into a bash script. There might be just one Batch* file or many, thus the need for a loop. mv would be my first choice but fails after trying to change one var to another. I've tried the "line" variable with and without the quotes.

ls Batch* | while read line
do
change=`echo ${line}| sed -e 's/ /_/g'`
mv \"$line\" $change
done

Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Dennis Handly
Acclaimed Contributor

Re: Change filename with awk

>I've tried the "line" variable with and without the quotes.
mv \"$line\" $change

Change to actually use quotes: mv "$line" $change
John Carver
Frequent Advisor

Re: Change filename with awk

Thanks for the help. My script can now rename the filenames.