Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rename a mulitple file based with specific porting in a file name

Status
Not open for further replies.

Kishore Nandagopal

Programmer
Mar 2, 2017
4
IN
Dear team ,

I have a list of file name , I need to replace date which i have mentioned in the below files. the passing input is 20160101.

File
Capture_khajma.jpg

GE_AML_CUSTTADF_20160824.CSV
GE_AML_DAYSTSDF_20160824.CSV
GE_AML_DEPTTSDFS_20160824.CSV
GE_AML_SECMTSS_20160115.CSV


Need O/P

GE_AML_CUSTTADF_20160101.CSV
GE_AML_DAYSTSDF_20160101.CSV
GE_AML_DEPTTSDFS_20160101.CSV
GE_AML_SECMTSS_20160101.CSV
 
Hi

Such renaming is the simplest to do with the [tt]rename[/tt] command from the File::Rename Perl package.

( Note that there is no standard [tt]rename[/tt] command on Unix/Linux. Some distributions are shipping a [tt]rename[/tt] command provided by the util-linux package, which has different syntax and is not helpful in this case. If you find no [tt]rename[/tt] command on your machine, check also [tt]rename.pl[/tt] as occasionally is shipped with that name. )

Code:
[blue]master #[/blue] ls -1
GE_AML_CUSTTADF_20160824.CSV
GE_AML_DAYSTSDF_20160824.CSV
GE_AML_DEPTTSDFS_20160824.CSV
GE_AML_SECMTSS_20160115.CSV

[blue]master #[/blue] rename.pl 's/\d{8}/20160101/' *.CSV

[blue]master #[/blue] ls -1
GE_AML_CUSTTADF_20160101.CSV
GE_AML_DAYSTSDF_20160101.CSV
GE_AML_DEPTTSDFS_20160101.CSV
GE_AML_SECMTSS_20160101.CSV
Assuming the only occurrence of 8 consecutive digits is in the date to be replaced.

In case you not have [tt]rename[/tt], you will have to use [tt]mv[/tt] in a loop and involve the shell's capabilities to craft the new file names :
Code:
[b]for[/b] file [b]in[/b] [teal]*.[/teal]CSV[teal];[/teal] [b]do[/b] mv [i][green]"$file"[/green] [green]"${file%_*}_20160101.${file##*.}"[/green][/i][teal];[/teal] [b]done[/b]
Assuming the date is always after the last underscore ( _ ) character.

In case you not have a Bourne shell compatible shell that understands the above command, pleas provide details about what you have.


Feherke.
feherke.ga
 
Need one more help from above ques?.. how to extract date only in the file name.

Needed o/p..

20160824
20160115
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top