- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - Linux
- >
- System Administration
- >
- Change filename with awk
-
- Forums
-
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Middle East
- HPE Blog, Russia
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
-
Blogs
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Blog, Latin America
- HPE Blog, Middle East
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
-
Information
- Community
- Welcome
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Tips and Tricks
- Resources
- Announcements
- Email us
- Feedback
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Blogs
-
Information
-
English
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- Report Inappropriate Content
10-12-2009 10:25 AM
10-12-2009 10:25 AM
Re: Change filename with awk
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2021 Hewlett Packard Enterprise Development LP