Desktop productivity for business analysts and programmers

rename command in a UNIX environment

Reply
New Contributor
Posts: 2

rename command in a UNIX environment

I would like to rename multiple file extensions from EU to txt using the below code result  but I dont get the desired results.

Please note that I am using the UNIX environment.

 

DATA _NULL_;
X 'rename /EIP/Data/Lev1/Data/FBM/.*EU /EIP/Data/Lev1/Data/FBM/*.txt';
RUN;

Super User
Posts: 5,355

Re: rename command in a UNIX environment

A couple of items to fix this ...

 

First, note that SAS does not require a DATA step in order to run the X command.  You only need one line of code.

 

Second, note that you need a legitimate Unix command.  There is no "rename" command in Unix.  There is both "mv" and (I believe) "rnm".  So get your command working in Unix first, without using SAS.  Then it should be straightforward to convert it to a SAS X command.

Super User
Posts: 11,107

Re: rename command in a UNIX environment

Also you may want to double check the location of the wildcard character * in the source part of the command

Respected Advisor
Posts: 3,786

Re: rename command in a UNIX environment

Do you really need to change the extension?  What will having the new extention get you that EU cannot provide.

New Contributor
Posts: 2

Re: rename command in a UNIX environment

I did not know that you can an EU file using SAS, just tried it and it worked.

Really appreciated... But I would also like to know how to rename multiple files that resides in the same UNIX directory.

 

I used the below piece of code in windows environment and it worked..

 

filename ren pipe "dir C:\Users\Desktop\FBM\FILES\NEW\*.EU /b /s";
/*To get the corresponding new file names*/
data ren;
infile ren;
input old : $250.;
new=tranwrd(old,".EU",'.TXT');
run;
/*To rename them all*/
data _null_;
set ren;
rc=rename(old, new, 'file');
put rc;
run;

Respected Advisor
Posts: 3,786

Re: rename command in a UNIX environment

I think a similar program using SAS RENAME function would work in UNIX just as well as Windows.

 

I was fiddling with this one line bash script.  It does not exectue 'MV' just shows the command with ECHO.  It's really the same as your SAS program only different language.  I think most folk would do some kind of GREP thing but I'm not good with that.

 

  •  for file in x*.xml; do echo "Process: $file"; x=`basename $file .xml`; echo "mv $file ${x}.txt"; done;
  • For each file that matches the pattern x*.xml
  • print the file name
  • assign x the filename with .xml removed
  • print the mv command 
>for file in x*.xml; do echo "Process: $file"; x=`basename $file .xml`; echo "mv $file ${x}.txt"; done;
Process: xmltest.xml
mv xmltest.xml xmltest.txt
Process: xp1.xml
mv xp1.xml xp1.txt
Process: xp2.xml
mv xp2.xml xp2.txt

Assumes UNIX shell is BASH

Ask a Question
Discussion stats
  • 5 replies
  • 438 views
  • 0 likes
  • 4 in conversation