- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: converting filenames
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
Forums
Discussions
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
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
07-19-2006 01:52 PM
07-19-2006 01:52 PM
i have 2 inquiries.
1. is it possible to convert the filename from lowercase to uppercase? Is this possible using awk or sed or perl?
2. i have a filename with the following pattern.
AAA_BB_20Jul2006_1234.TXT
is it possible to rename it using awk, perl or sed to:
AAA-BB_20Jul2006_1234.TXT?
Maximum points to all correct answers.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2006 02:19 PM
07-19-2006 02:19 PM
Re: converting filenames
However an easier approach is to use tr.
#!/usr/bin/sh
typeset OLD_FNAME=""
typeset NEW_FNAME=""
typeset -i STAT=0
while [[ ${#} -ge 1 ]]
do
OLD_FNAME=${1}
shift
if [[ -r "${FNAME}" ]] # file exists
then
NEW_FNAME=$(echo "${OLD_FNAME}" | tr "[a-z]" "[A-Z]")
if [[ "${NEW_FNAME}" != "${OLD_FNAME}" ]]
then
if [[ -r "${NEW_FNAME}" ]]
then
echo "${NEW_FNAME} already exists." >&2
else
mv "${OLD_FNAME}" "${NEW_FNAME}"
STAT=${?}
fi
fi
else
echo "${OLD_FNAME} not found." >&2
fi
done
exit ${STAT}
----------------------------------
Your second question is exactly the same except:
NEW_FNAME=$(echo "${OLD_FNAME}" | tr "[a-z]" "[A-Z]")
becomes:
NEW_FNAME=$(echo "${OLD_FNAME}" | tr "-" "_")
Use it like: myscript.sh filename1 filename2 ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2006 04:27 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2006 06:39 PM
07-19-2006 06:39 PM
Re: converting filenames
# ls -1
>2. from...AAA_BB_20Jul2006_1234.TXT
to...AAA-BB_20Jul2006_1234.TXT
# ls -1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2006 02:45 PM
07-20-2006 02:45 PM
Re: converting filenames
Hi Hein,
What do you mean by the $old in the perl command line?
Could you explain it to me?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2006 02:59 PM
07-20-2006 02:59 PM
Re: converting filenames
The top structure here is:
perl -e 'blah blah' argument(s)
So $old is not on the command line, but within the 'one-liner' program 'blah blah'.
The $old variable in the program is picked up from the first argument passed: the file name to be changed.
I just used a 'shift @ARGV' to get the value.
That's your first argument for the rename.
The second argument is the un-named 'new' name computed as uppercase on $old using uc($old).
Clear?
Hein.