- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Change filename with awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2009 01:31 PM
тАО10-08-2009 01:31 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2009 02:51 PM
тАО10-08-2009 02:51 PM
Re: Change filename with awk
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2009 03:04 PM
тАО10-08-2009 03:04 PM
SolutionOops; 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2009 04:28 PM
тАО10-08-2009 04:28 PM
Re: Change filename with awk
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2009 09:04 PM
тАО10-08-2009 09:04 PM
Re: Change filename with awk
Much better to use mmv or prename (might be called something else in your distribution - it's a handy Perl script).
Regards,
Goran
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2009 07:53 AM
тАО10-09-2009 07:53 AM
Re: Change filename with awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2009 10:30 AM
тАО10-09-2009 10:30 AM
Re: Change filename with awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-10-2009 06:34 PM
тАО10-10-2009 06:34 PM
Re: Change filename with awk
mv \"$line\" $change
Change to actually use quotes: mv "$line" $change
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2009 10:25 AM
тАО10-12-2009 10:25 AM