Change the File name using pattern and number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change the File name using pattern and number
# 1  
Old 02-09-2011
Tools Change the File name using pattern and number

Hi,

i have the below files in the wrk folder. File names are
Code:
AAB0001.R
BBA0002.R
CCDC0003.R
AAB0002.R
AAB0034.R
BBA0081.R
DDDE0008.R

i need to change the file name USING pattern and number. output like below Smilie
Code:
AAB0001.R
AAB0002.R
AAB0003.R
BBA0001.R
BBA0002.R
CCDC0001.R
DDDE0001.R

Thanks in advance.

Last edited by Franklin52; 02-10-2011 at 04:18 AM.. Reason: Please use code tags
# 2  
Old 02-09-2011
Code:
ls wrk/*.R | awk '
{
    sub("^wrk/", "");
    sub(".R$", "");
    pre=post=$0;
    sub("[0-9]*$", "", pre);
    sub("^[A-Z]*", "", post);
    new=sprintf("%04d", ++P[pre]);
    if (new != post) system("mv wrk/" pre post ".R wrk/" pre new ".R")
}'

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 02-10-2011
Quote:
Originally Posted by Chubler_XL
Code:
ls wrk/*.R | awk '
{
    sub("^wrk/", "");
    sub(".R$", "");
    pre=post=$0;
    sub("[0-9]*$", "", pre);
    sub("^[A-Z]*", "", post);
    new=sprintf("%04d", ++P[pre]);
    if (new != post) system("mv wrk/" pre post ".R wrk/" pre new ".R")
}'


Thanks ! it is working...Really Great.....Smilie

Can you Please explain each line...because i don't know awk command in unix...
# 4  
Old 02-10-2011
sub("^wrk/", ""); Replace "wrk/" from begining of line with nothing (remove)
sub(".R$", ""); Remove ".R" from end of line (leaving just filename without extension
pre=post=$0; copy line (after replaces above) to pre and post variables
sub("[0-9]*$", "", pre); remove all numerics from end of pre
sub("^[A-Z]*", "", post); remove all upper case alphas from front of post
new=sprintf("%04d", ++P[pre]); Increment a hash array counter with value of pre as it's index.
  Then format this count as 4 digits with leading zeros and put into variable new
if (new != post) system("mv wrk/" pre post ".R wrk/" pre new ".R") if new not equal to post then call system to rename file as required.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change comma delimeter in the file to number?

I have a file H,20180624200732,VPAS,TRANS_HDR,20180724, VPAS.TRANS_HDR.20180724.01.txt, ,93, T,1, I have to change and instead first comma put ",1" like below H,20180624200732,VPAS,TRANS_HDR,20180724, VPAS.TRANS_HDR.20180724.01.txt,1,93, T,1, I made sed "2s/, /,1/"... (8 Replies)
Discussion started by: digioleg54
8 Replies

2. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

4. UNIX for Dummies Questions & Answers

How to find count total number of pattern in a file …?

How to find count total number of pattern in a file … File contains : a.txt ------------- aaa bbb nnn ccc aaa bbb aaa ddd aaa aaa aaa aaa grep -c aaa a.txt Op: 4 ( But my requirement is should count the total no of patterns as 7 ) (4 Replies)
Discussion started by: Jitten
4 Replies

5. UNIX for Advanced & Expert Users

Count number of lines between a pattern in a large file

1000CUS E Y4NYRETAIL 10010004HELIOPOLIS 110000500022360591000056XX EG 1101DEBY XXAD ZSSKY TSSROS 1102HANYNNYY@HOTMAIL.COM 210030/05/201301/06/2013AED 3100 OPE 3100 CLO 3100 The 1000CUS E Y NYCORPORATE 10010004HELIOPOLIS 110000500025270504550203XX EG 1101XXXQ FOR CTING AND... (1 Reply)
Discussion started by: john2022
1 Replies

6. 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

7. Shell Programming and Scripting

How to display the line number of file while searching for a pattern

awk 'BEGIN{IGNORECASE=1} /error|warning|exception/ { ++x } END { print x }' filename The above command returning the number of times the pattern present in the file. But I want the the line number as well. please help me out (6 Replies)
Discussion started by: arukuku
6 Replies

8. UNIX for Dummies Questions & Answers

how to change a particular value in a file based on a pattern

how to change a particular value in a line of a file based on a pattern eg: source file will be like ================= 99999999999 maximum number(0) ksuisikjjsl;;l skjss''llsl minimum number 0000000 maximum number of golds(0) target should be like ================= ... (9 Replies)
Discussion started by: orbeyen
9 Replies

9. Shell Programming and Scripting

how to change a number in a file with sed?

Hello, I have a file which contains some line as follows, 9.9 TEMP 9.9 MUCOEFF 0.0 EPSILON And I want to increase this MUCOEFF by 0.2 as 9.9 TEMP 10.1 MUCOEFF 0.0 ... (7 Replies)
Discussion started by: lorenzz
7 Replies

10. Shell Programming and Scripting

replace character in a string pattern and save the change in same file

I am facing one problem, can any one please suggest me the command for the same in unix. I am using Ksh. I have a large file with the data that looks like below. "ROTO2-2007f","_US01","9/15/2007","9/21/2007",346492,"NICK, LCD WATCH"97,1,"NAPOLITJ ","BERGER,M Z & CO INC",0.01, ... (2 Replies)
Discussion started by: mihir0011
2 Replies
Login or Register to Ask a Question