- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- renaming (or appending to) only a part of a string
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
03-01-2007 11:30 AM
03-01-2007 11:30 AM
I need some ideas about renaming some part of a string.
I have text file which have directory names in it, like this:
/usr/bin/dummyfile.dat
/var/local/src/file.finder.2000.dat
/var/spool/printerqueue.dat
/home/ICS/U990702043/The man who saves the world.dat
...
...
...
I need to append a different string to these strings and save the results to a different or the same text file. Appending (or renaming) must be on the last name, or after the last slash (/). For example, I want to append 'here_is_' string. Results should be like:
/usr/bin/here_is_dummyfile.dat
/var/local/src/here_is_file.finder.2000.dat
/var/spool/here_is_printerqueue.dat
/home/ICS/U990702043/here_is_The man who saves the world.dat
If someone would help I would be most grateful.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2007 12:08 PM
03-01-2007 12:08 PM
Re: renaming (or appending to) only a part of a string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2007 01:05 PM
03-01-2007 01:05 PM
Re: renaming (or appending to) only a part of a string
insert that string. Your above paths seem to have differing number of "/" and you want the last one.
>how can I append after the last '/'?
Just use substr in a loop to find the last one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2007 01:16 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2007 01:18 PM
03-01-2007 01:18 PM
Re: renaming (or appending to) only a part of a string
:)
while read line
do
print `dirname "${line}"`/here_is_`basename "${line}"`
done < infile.txt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2007 08:50 PM
03-01-2007 08:50 PM
Re: renaming (or appending to) only a part of a string
#!/usr/bin/sh
string=`echo "here_is_" |rev`
rev dat.lis | sed "s/\//${string}\//" | rev
Method:
Reverse the data to be inserted
For each line in the file dat.lis
reverse the line
subsitute the (now first) instance of '/' with the new data
reverse the line to 'normal'
Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2007 08:57 PM
03-01-2007 08:57 PM
Re: renaming (or appending to) only a part of a string
just for the choice, here is another way:
sed 's/\(.*\)\//\1\/here_is_/' dat.lis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2007 09:26 PM
03-01-2007 09:26 PM
Re: renaming (or appending to) only a part of a string
echo
for(i=1;i
str=p"/here_is_"$NF
system("mv "$0" "str)
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2007 11:40 PM
03-01-2007 11:40 PM
Re: renaming (or appending to) only a part of a string
you get a more readable version of Peter G.'s solution in NOT using '/' as sed command-delimiter:
sed 's:\(.*\/):\1here_is_:' dat.lis
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2007 12:31 AM
03-02-2007 12:31 AM
Re: renaming (or appending to) only a part of a string
#!/bin/sh
while read oldname
do
directory=$(dirname "$oldname")
filename=$(basename "$oldname")
newname="${directory}/here_is${filename}"
echo "$newname"
done
Pipe the text file into this script, and you'll get the modified version to standard output.
For example:
sh script.sh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2007 12:33 AM
03-02-2007 12:33 AM
Re: renaming (or appending to) only a part of a string
No points for me from my previous answer, thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2007 07:44 PM
03-05-2007 07:44 PM
Re: renaming (or appending to) only a part of a string
you have a whole range of solutions here. One-liners to mini-scripts.
If these answers helped you with solution(s) could you please complete the thread by awarding points to helpful answers and summarising the solution for you.
This will help resolution of similar problems in the future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2007 01:30 AM
03-06-2007 01:30 AM
Re: renaming (or appending to) only a part of a string
sorry for the delay.