Multiple File Rename based on pattern - one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple File Rename based on pattern - one line
# 1  
Old 05-30-2013
Multiple File Rename based on pattern - one line

All,

I wanted to copy the files

From:

Daily_XYZ_TEST_1.csv
Daily_XYZ_TEST_2.csv
Daily_XYZ_TEST_3.csv
Daily_XYZ_TEST_4.csv


To:

Daily_ABC_TEST_1.csv
Daily_ABC_TEST_2.csv
Daily_ABC_TEST_3.csv
Daily_ABC_TEST_4.csv



I have tried the rename command but it is not working
rename 's/*XYZ*/*ABC*/' *.csv

Any advice welcome
# 2  
Old 05-30-2013
First, there is no utility named rename that is callable from the shell on most systems. If you're talking about the perl rename function, it would be a good idea to state that you're having a problem with perl rather than making us guess.

Assuming this is a statement in a perl script, I think you want to change:
Code:
rename 's/*XYZ*/*ABC*/' *.csv

to:
Code:
rename 's/_XYZ_/_ABC_/' *.csv

PS Note that rename does not copy files, it renames (or moves) them.

Last edited by Don Cragun; 05-30-2013 at 08:00 PM..
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-30-2013
Thank you for the reply. Unfortunately I was looking up some results on google and did not check that rename is only available in perl.

I would like to see if there was a one line command that i could use in the shell to perform my pattern based rename.
# 4  
Old 05-30-2013
ahem: what you did was throwing a "sed"-command at the shell. Big surprise, being not "sed" the shell was confused. Talk japanese to any average middle-european and you are bound to get the same reaction. ;-))

What you have to do is: set up a loop, looping through the filenames and storing these in a shell variable. Then change the contents of the shell variable to reflect the new name for the respective file. Finally, using the old and the new value of the variable, use the source- and target-names in a "mv" command to rename the files:

Code:
ls Daily_XYZ_TEST_*.csv | while read SRCFILE ; do
     IDX=${SRCFILE#Daily_XYZ_TEST_}   # cut off "Daily_XYZ_TEST_" from front
     IDX=${IDX%.csv}                  # cut off ".csv" from end
     mv "$SRCFILE" "Daily_ABC_TEST_${IDX}.csv"
done

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 05-30-2013
If you're using a recent bash or a ksh newer than its 1988 version as your shell, you could also use:
Code:
for i in Daily_XYZ_TEST_*.csv
do      mv "$i" "${i/_XYZ_/_ABC_}"
done

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 05-31-2013
Twenty years ago I met "mmv.c", "multi-move".
The compiled "mmv" needed in the substitution =1 =2 for each corresponding wild card * or ?
In your case
Code:
mmv "*XYZ*.csv" "=1ABC=2.csv"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Advanced & Expert Users

Concatenation of multiple files based on file pattern

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

3. UNIX for Beginners Questions & Answers

Consternation of multiple file names based on naming pattern

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

4. Shell Programming and Scripting

Split a text file into multiple pages based on pattern

Hi, I have a text file (attached the sample). I have also, attached the way the way the files need to be split. We get this file, that will either have 24 Jurisdictions, or will miss some and retain some. Like in the attached sample file, there are only Jurisdictions 03,11,14,15, 20 and 30.... (3 Replies)
Discussion started by: ebsus
3 Replies

5. Shell Programming and Scripting

split XML file into multiple files based on pattern

Hello, I am using awk to split a file into multiple files using command: nawk '{ if ( $1 == "<process" ) { n=split($2, arr, "\""); file=arr } print > file }' processes.xml <process name="Process1.process"> ... (3 Replies)
Discussion started by: chiru_h
3 Replies

6. Shell Programming and Scripting

Rename Script based on Pattern

Hi I am writing a Rename file Script based on the pattern , like script.sh file*2 file*xx my script should rename all the files which matches file*2 and rename them to file*xx iPattern=$1 oPattern=$2 Array=$(ls $iPattern) for i in $Array do mv $i XXXXXXX << could not get here >> ... (2 Replies)
Discussion started by: rakeshkumar
2 Replies

7. UNIX for Dummies Questions & Answers

print multiple lines from text file based on pattern list

I have a text file with a list of items/patterns: ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig12238 ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig34624... (1 Reply)
Discussion started by: Oyster
1 Replies

8. Shell Programming and Scripting

Splitting large file into multiple files in unix based on pattern

I need to write a shell script for below scenario My input file has data in format: qwerty0101TWE 12345 01022005 01022005 datainala alanfernanded 26 qwerty0101mXZ 12349 01022005 06022008 datainalb johngalilo 28 qwerty0101TWE 12342 01022005 07022009 datainalc hitalbert 43 qwerty0101CFG 12345... (19 Replies)
Discussion started by: jimmy12
19 Replies

9. Shell Programming and Scripting

Split a file into multiple files based on the input pattern

I have a file with lines something like. ...... 123_start ...... ....... 123_end .... ..... 456_start ...... ..... 456_end .... ..... 789_start .... .... 789_end (6 Replies)
Discussion started by: abinash
6 Replies

10. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies
Login or Register to Ask a Question