LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2017, 10:27 AM   #1
nickz2017
LQ Newbie
 
Registered: Oct 2017
Posts: 1

Rep: Reputation: Disabled
Replacing multiple characters across multiple File names in a Unix folder


I have a couple of files in a Unix folder, let's say /home/TRANS.
Files are received within this folder on a monthly basis. File names are like:
  • ENCD_213_E-DM_CCA_ID3490_A01.txt
  • ENCD_213_E-DM_CCA_ID33120_A01.txt
  • ENCD_213_E-DM_CCA_IDP3664_A01.txt
  • ENCD_213_E-DM_CCA_ID3327_A01.txt
  • ENACT_215_E_DM_CCA_IDA33320_25OCT2017.csv
  • ENACT_215_E_DM_CCA_IDA31116_25OCT2017.csv
After renaming, the final output should be:
  • id3490.txt
  • id33120.txt
  • idp3664.txt
  • id3327.txt
  • ida33320.csv
  • ida31116.csv
So essentially, I want to have the following strings replaced across all file names within the TRANS folder, and make the final output filename as lowercase:
  • ENCD_213_E-DM_CCA_
  • _A01
  • ENACT_215_E_DM_CCA_
  • _25OCT2017
How can I execute this either in a single line command or as a shell script? I did check multiple questions but couldn't find replacement of multiple characters in addition to changing to lowercase. Need your help as I am pretty new to Unix.
 
Old 10-25-2017, 03:46 PM   #2
IsaacKuo
Senior Member
 
Registered: Apr 2004
Location: Baton Rouge, Louisiana, USA
Distribution: Debian Stable
Posts: 2,396
Blog Entries: 8

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
I use a utility called "mmv". How you install "mmv" depends on what linux distribution you use (or whatever OS you use...you say "Unix", which suggests you're not using Linux).

For example, with Debian it's simply

Code:
apt-get install mmv
For your purposes, a way to do it could be:
Code:
mmv "ENCD_213_E-DM_*_A01.*" "#l1.#l2"
mmv "ENACT_215_E_DM_CCA_*_*.*" "#l1.#l3"
The basics for using mmv are:

mmv "sourcepattern" "destpattern"

The sourcepattern is pretty intuitive - you use * to match any number of characters, and ? to match any single character.

Then, imagine numbering each wildcard from left to right:

Code:
ENACT_215_E_DM_CCA_*_*.*
                   1 2 3
This lets you map output in the destpattern using #1, #2, #3, and so on. Additionally, you can convert to lowercase with #l1, #l2, #l3, and so on; you can convert to uppercase with #u1, #u2, #u3, and so on.

When you run mmv, it will test the destination files before doing anything. If there are any name collisions, it will FAIL SAFE! It will do no changes to any files, and it will give you an error saying there were destination name collisions.

That is, to me, the killer feature of mmv. You can use it with confidence that you won't lose files because renamed files overwrote. And you won't have to worry about some fancy script messing up in the middle of things, leaving some files renamed and others not renamed.

Basically, mmv is a wonderful little tool for these tasks.
 
1 members found this post helpful.
Old 10-25-2017, 06:04 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,821

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
I have not used mmv and does look like a good little tool (will have to check it out), but you could also write a simple bash script as well.
 
Old 10-25-2017, 06:06 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,788

Rep: Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574
Looks a bit odd, but definitely a keeper.
Thanks for the explanation - I've seen it mentioned before, but never botherered looking into it.
 
Old 10-26-2017, 12:37 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,690
Blog Entries: 3

Rep: Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920Reputation: 2920
Quote:
Originally Posted by IsaacKuo View Post
That is, to me, the killer feature of mmv. You can use it with confidence that you won't lose files because renamed files overwrote.
Thanks. I hadn't seen mmv either. I usually use the perl version of rename. It has the -n option to do a dry run first, so you can check the results before applying the. Also, since it handles full perl expressions, functions like printf() and even variables are allowed, too. So I've used it for renumbering files. But I'm definitely looking closer at mmv
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tar multiple folder to multiple files to another directory and delete original folder zeroize Linux - Newbie 6 03-31-2016 07:36 PM
[SOLVED] create folder based on first three characters of file names and move matching files koshy Linux - Newbie 6 11-02-2015 08:11 AM
[SOLVED] replacing characters only within a string of length 30 in multiple files kdo Linux - Newbie 16 03-03-2014 09:47 AM
[SOLVED] Find statement for multiple folder names anon091 Linux - Newbie 15 01-21-2010 07:29 PM
Replacing characters in file names? yuccabrevifolia Linux - Software 1 04-19-2006 01:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration