Operating System - HP-UX
1830239 Members
2101 Online
109999 Solutions
New Discussion

UNIX - remembering strings from wild characters

 
SOLVED
Go to solution
Raynald Boucher
Super Advisor

UNIX - remembering strings from wild characters

Good day all?

Ist there a way to issue a command that will expand to the following command while using wild characters?

mv ectst_123.xxx prever_123.xxx

ex: mv ectst_*.xxx prever_(???).xxx

In other words, I'm trying to remember what the wild characters expanded to and use it in my destination filename.

Is there an alternate way?

Thanks.
7 REPLIES 7
Rodney Hills
Honored Contributor

Re: UNIX - remembering strings from wild characters

For what you are trying to accomplish the standard wildcard mechanism won't work. I have attached a tar file with a command called "ren" and its' associated man page.

Read the man page carefully and I think you'll find this will do what you want.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: UNIX - remembering strings from wild characters

whoops, somehow the attachment didn't make it.

Here it is

-- Rod Hills
There be dragons...
S.K. Chan
Honored Contributor
Solution

Re: UNIX - remembering strings from wild characters

Assuming the only thing you want to do is replace "ectst" with "prever" in the filename, then you can do something like this .. from the command line ..(cd to where those files are)..
$ for f in $(ls ectst_*)
> do
> a=$(echo $f|sed s/ectst/prever/g)
> mv $f $a
> done
Test it first with a few dummy files before you actually run it.
Rodney Hills
Honored Contributor

Re: UNIX - remembering strings from wild characters

If you want to use the standard tools (and I include "perl" as a standard tool), then here is a short perl script-

cd yourdirectory
ls | perl -ne 'if (/ectst_(.*?)\.xxx/) { system("mv $_ prever_\1.xxx")}'

This would require you to use regular expressions rather than filename patterns. It would give you more flexibility though...

It could be dangerous if you enter in an incorrect regular expression. It might be better to output the "mv" commands to a file for pre-inspection before executing.

ls | perl -e 'if (/ectst_(.*?)\.xxx/) { print "mv $_ prever_\1.xxx"}' >move.script.sh

HTH

-- Rod Hills
There be dragons...
Raynald Boucher
Super Advisor

Re: UNIX - remembering strings from wild characters

Thank you both.
Rodney, I haven't had time to review that tar file yet but will do... sounds interesting.
S.K., Will use your solution for now.

Take care.
Rodney Hills
Honored Contributor

Re: UNIX - remembering strings from wild characters

Change
ls | perl -ne 'if (/ectst_(.*?)\.xxx/) { system("mv $_ prever_\1.xxx")}'

to
ls | perl -ne 'if (/ectst_(.*?)\.xxx/) { system("mv $_ prever_$1.xxx")}'

I forgot \1 is only effective in a s///.

-- Rod Hills


There be dragons...
Jordan Bean
Honored Contributor

Re: UNIX - remembering strings from wild characters


Nope. The shells just aren't that smart.

In POSIX and Korn shells, do it this way:

for file in ectst_*
do
mv $file prever_${file##*_}
done