How to change file name using shell ?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to change file name using shell ?
# 1  
Old 02-10-2010
How to change file name using shell ?

Hi..

I have a list of input files as below:-
Code:
NXUPM.P999309.201002011144
NXUPM.P999409.201002011145
NXUPM.P999509.201002011146
NXUPM.P999609.201002011146
NXUPM.P999682.201002011201

I want to change it as below:-
Code:
NXUPM.P999309_999408
NXUPM.P999409_999508
NXUPM.P999509_999608
NXUPM.P999609_999681
NXUPM.P999682_999781

Actually, the output file naming conversion as below:-
Code:
NXUPM.<first sequence>_<last sequence>

The last sequence = first sequence + 99

Please help me

Thank you,
Baharin
# 2  
Old 02-10-2010
Hensem I hope this works, i havent tested the code:

Code:
awk -F\. 'gsub("^[a-zA-Z]","",$2) {print $1".P"$2"_"($2)+99}' URFILE

# 3  
Old 02-10-2010
Code:
ls -1 | awk -F"." '{
t1=substr($2,1,4);
t2=substr($2,5);
print "mv "$0 " "$1"."$2"_" t1 t2+99}'

if this gives what you want...you can run below...

Code:
ls -1 | awk -F"." '{
t1=substr($2,1,4);
t2=substr($2,5);
print "mv "$0 " "$1"."$2"_" t1 t2+99}' | sh

# 4  
Old 02-10-2010
If you have rename command, then use this.
Code:
rename 's/(\d+)\.(\d+)$/$1 . "_" . ($1+99)/e' *

Code:
$ ls -l
total 0
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999309.201002011144
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999409.201002011145
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999509.201002011146
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999609.201002011146
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999682.201002011201
$ rename 's/(\d+)\.(\d+)$/$1 . "_" . ($1+99)/e' *
$ ls -l
total 0
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999309_999408
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999409_999508
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999509_999608
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999609_999708
-rw------- 1 sathiyamoorthy users 0 Feb 10 17:07 NXUPM.P999682_999781

# 5  
Old 02-10-2010
You can also install the 'mmv' tool, which has convenient globbing-based file renaming features.
# 6  
Old 02-13-2010
As I am thinking of the same.. its pleasure to have the answer from your corner..

Keep sharing new things.. and be informative as ever..

Regards,
Roddick..

.

Last edited by vbe; 02-15-2010 at 04:59 AM.. Reason: rm URL
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script change new format on the file.

Hi---Is there's way can write small shell script or perl script open "abc.txt" file and create new "new_abc.txt" file with format output below? Thanks cat abc.txt ###########################Readme############################### Contained with this README.TXT file are all of the file... (7 Replies)
Discussion started by: dotran
7 Replies

2. Shell Programming and Scripting

Need Help:Shell Script for Solaris to change the dates in a file by one week

I have to increase the date by one week in an input when script is executed in solaris. I was able to acheive this using ksh script that is working in Linux enivironment, when i execute the same script in Solaris i am getting below error: /var/tmp\n\r-> ./script.ksh date: illegal option -- d... (3 Replies)
Discussion started by: sriramanaramoju
3 Replies

3. Shell Programming and Scripting

Change value in a file using perl or shell script

hi, I have a local.conf file which has the first line TOPDIR = "/home/mvdev/workspace/boxer". I want to replace the value to "/home/common/workspace/mirror". I tried the following perl command that is perl -p -i -e 's/Path/path1/g' myfile.txt then sed... (7 Replies)
Discussion started by: amvarma77
7 Replies

4. Shell Programming and Scripting

shell script to change the extension of a file

I have a directory that contains several files, out of which some files are have an extra extension for example file1.new.new.new file2.new.new.new file3.new.new.new file4.new.new.new i want to write a shell script that rename all such file with only single extension like file1.new... (7 Replies)
Discussion started by: mukulverma2408
7 Replies

5. Shell Programming and Scripting

How to change a number on a specific lines in a file with shell?

Hello My problem is that I want to change some specific numbers in a file. It is like, 2009 10 3 2349 21.3 L 40.719 27.388 10.8 FRO 7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1 GAP=157 1.69 5.7 5.9 5.8 0.5405E+01 0.4455E+00 0.1653E+02E STAT SP IPHASW D HRMM SECON CODA AMPLIT... (11 Replies)
Discussion started by: miriammiriam
11 Replies

6. Shell Programming and Scripting

ksh; Change file permissions, update file, change permissions back?

Hi, I am creating a ksh script to search for a string of text inside files within a directory tree. Some of these file are going to be read/execute only. I know to use chmod to change the permissions of the file, but I want to preserve the original permissions after writing to the file. How can I... (3 Replies)
Discussion started by: right_coaster
3 Replies

7. Shell Programming and Scripting

Change the font of text in output file in shell scipt

hi, I want to change the font of text in output file. :( I tried the below code code: if awk 'BEGIN{if('$RSS'>='1000')exit 0;exit 1}' then RED=`echo "\033 i can see colors in terminal but not in output file :wall: please help me how i can get colors text in output file. edit... (1 Reply)
Discussion started by: sreelu
1 Replies

8. Shell Programming and Scripting

Help with shell script for know when a file change it

Hi, I´d like to know how to program a shell script for know when a file changes and based on that make another tasks all this in real time.. Thanks (2 Replies)
Discussion started by: mrios7
2 Replies

9. Shell Programming and Scripting

Change a line in a php file thanks to a shell script

Hi, I'm working on a script to make automatic the new releases of my website... However in this script I put all the css script in a single one. There's no rpoblem for that. My problem is when I want to change the header of my layout page to put instead of : $header.="<link rel=\"stylesheet\"... (2 Replies)
Discussion started by: lahabana
2 Replies

10. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies
Login or Register to Ask a Question