- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Removing multiple bad characters from files
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
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
тАО01-27-2009 12:33 PM
тАО01-27-2009 12:33 PM
Example BAD:
Testfile(1000171)20090112_154012.xml
Monthly Data_1000171_12-07-2008_2009-01-20_11.13.27.xml
Example Good:
Testfile_1000171_20090112.xml
Monthly_Data_1000171_20090120.xml
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 12:52 PM
тАО01-27-2009 12:52 PM
Solutionbash$ echo 'Testfile(1000171)20090112_154012.xml' | \
sed -e 'y/() /___/'
Testfile_1000171_20090112_154012.xml
What's bad about hyphens? You can certainly
remove or translate them, but they're not
shell-special. Why not leave them?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 01:16 PM
тАО01-27-2009 01:16 PM
Re: Removing multiple bad characters from files
Note that Steven used the transform operator (function) equivalent to 'tr' in the shell. This is much faster than an equivalent substition operation -- a nice touch here.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 01:55 PM
тАО01-27-2009 01:55 PM
Re: Removing multiple bad characters from files
Here is a PERL alternative:
First a dry run:
$ touch "a(b)c.tmp"
$ touch "xa((b)c.tmp"
$ perl -e 'for (@ARGV) { $old=$_; if (s/[()-]+/_/g) { print qq(rename $old, $_ \n)} }' *.tmp
rename a(b)c.tmp, a_b_c.tmp
rename xa((b)c.tmp, xa_b_c.tmp
Remove the print( \n) to make it real:
perl -e 'for (@ARGV) { $old=$_; if (s/[()-]+/_/g) { print qq(rename $old, $_ \n)} }' *.tmp
$ ls *.tmp
a_b_c.tmp xa_b_c.tmp
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 02:12 PM
тАО01-27-2009 02:12 PM
Re: Removing multiple bad characters from files
I just noticed how the substitution is not only supposed to pick out 'odd' characters, but is also expected to do full transformations of a date format.
Much more fun, but much the same...
$ touch "Monthly Data_1000171_12-07-2008_2009-01-20_11.13.27.tmp"
$ touch "Testfile(1000171)20090112_154012.tmp"
$ perl -e 'for (@ARGV) { $old=$_; if (s/[()]+/_/g + s/(\d{4})-(\d\d)-(\d\d_\d\d\.\d\d\.\d\d/$1$2$3/g) { print qq(rename $old, $_ \n)} }' *.tmp
rename Monthly Data_1000171_12-07-2008_2009-01-20_11.13.27.tmp, Monthly Data_1000171_12-07-2008_2009
0120.tmp
rename Testfile(1000171)20090112_154012.tmp, Testfile_1000171_20090112_154012.tmp
So here we ADD the number of substitutions done by
1: s/[()]+/_/g
with those done by
2: s/(\d{4})-(\d\d)-(\d\d_\d\d\.\d\d\.\d\d/$1$2$3/
If the result is true (not-zero), then we call 'print' at first, and change to rename when confident.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 02:42 PM
тАО01-27-2009 02:42 PM