- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Replacing the file name
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
Forums
Discussions
Discussions
Discussions
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
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-13-2009 01:59 AM
10-13-2009 01:59 AM
Replacing the file name
In one dir i have files like:
0345_ad.csv
0346_ad.csv
1234_ad.csv
3456_ad.csv
0345_ng.csv
0346_ng.csv
1234_ng.csv
3456_ng.csv
3908_21.csv
etc..
Out of these files i want to change the file having ...._ng.csv to ...._NG.csv and same for ...._ad.csv to ...._AD.csv.
Can someone suggest how to do this?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2009 02:41 AM
10-13-2009 02:41 AM
Re: Replacing the file name
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2009 02:49 AM
10-13-2009 02:49 AM
Re: Replacing the file name
Run the following from your directory.
for i in `ls *_ng.csv | cut -f1 -d_`;do mv ${i}_ng.csv ${i}_NG.csv;done
for i in `ls *_ad.csv | cut -f1 -d_`;do mv ${i}_ad.csv ${i}_AD.csv;done
HTH,
REgards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2009 04:48 AM
10-13-2009 04:48 AM
Re: Replacing the file name
untested (in a rush):
First see if you can find and transform what you need with a PRINT
perl -e 'for (<*>){ $old=$_; m/_(.*)\./; $new = $`.uc($1).$'; print "$old, $new\n" }' | more
Next edit command to use RENAME for real.
perl -e 'for (<*>){ $old=$_; m/_(.*)\./; $new = $`.lc($1).$'; rename $old, $new }'
$1 is thestring matched between the parens ().
The $` is the'pre-match variable, $' = post-match.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2009 04:58 AM
10-13-2009 04:58 AM
Re: Replacing the file name
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2009 05:39 AM
10-13-2009 05:39 AM
Re: Replacing the file name
Hein's untested solution needs a minor correction as it loses the "_" and ".' characters. It also misses constructing a new filename when the old one doesn't match the pattern needed.
This variation corrects that and only prints or renames files that match the pattern:
#!/usr/bin/perl
for (<*>) {
$old = $new = $_;
m/(_.*\.)/ and $new = $`.uc($1).$';
print "$old $new\n" unless $new eq $old;
}
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2009 06:03 AM
10-13-2009 06:03 AM
Re: Replacing the file name
You could improve this by using only one loop and using sed:
for file in *_ng.csv *_ad.csv; do
mv $file $(echo $file | sed -e 's/_ng/_NG/' -e 's/_ad/_AD/')
done
(Check first by adding "echo " before the mv.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2009 06:38 AM
10-15-2009 06:38 AM
Re: Replacing the file name
for i in /file/path/*.csv; do
mv "$i" "$(echo $i | tr a-z A-Z)"
done
Best regards
Fredrik Eriksson