Operating System - HP-UX
1832161 Members
11589 Online
110038 Solutions
New Discussion

renaming (or appending to) only a part of a string

 
SOLVED
Go to solution

renaming (or appending to) only a part of a string

Hello again.

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.
12 REPLIES 12

Re: renaming (or appending to) only a part of a string

should I use awk, sed, or cut? how can I append after the last '/'? I am so confused...
Dennis Handly
Acclaimed Contributor

Re: renaming (or appending to) only a part of a string

It seems you need to use awk to see where to
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.
Denver Osborn
Honored Contributor
Solution

Re: renaming (or appending to) only a part of a string

There are several ways it could be done, this is just one.

while read line
do
print `dirname "${line}`/here_is_`basename "${line}"`
done < infile.txt


Hope this helps
-denver
Denver Osborn
Honored Contributor

Re: renaming (or appending to) only a part of a string

sorry, forgot a "

:)


while read line
do
print `dirname "${line}"`/here_is_`basename "${line}"`
done < infile.txt
Peter Godron
Honored Contributor

Re: renaming (or appending to) only a part of a string

Osman,

#!/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.

Peter Godron
Honored Contributor

Re: renaming (or appending to) only a part of a string

Osman,
just for the choice, here is another way:
sed 's/\(.*\)\//\1\/here_is_/' dat.lis

Sandman!
Honored Contributor

Re: renaming (or appending to) only a part of a string

Try the awk construct below:

echo | awk -F/ '{
for(i=1;i p=(p?p"/"$i:$i)
str=p"/here_is_"$NF
system("mv "$0" "str)
}'
Peter Nikitka
Honored Contributor

Re: renaming (or appending to) only a part of a string

Hi,

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
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Matti_Kurkela
Honored Contributor

Re: renaming (or appending to) only a part of a string

Here's a shell solution:

#!/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 modified.txt
MK
Matti_Kurkela
Honored Contributor

Re: renaming (or appending to) only a part of a string

Oh, sorry, seems Denver Osborn already showed this way.

No points for me from my previous answer, thanks!
MK
Peter Godron
Honored Contributor

Re: renaming (or appending to) only a part of a string

Hi Osman,
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.

Re: renaming (or appending to) only a part of a string

of course, i will.

sorry for the delay.