Remove repeating pattern from beginning of file names.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove repeating pattern from beginning of file names.
# 1  
Old 01-14-2010
Remove repeating pattern from beginning of file names.

I want a shell script that will traverse a file system starting at specific path.

And look at all file names for repeating sequences of [number, number, space] and remove them from the file name.

The portion of the name that gets removed has to be a repeating sequence of the same characters.

So the script would start from a specific directory, for example:

/Users/username/Music/

And traverse the file system looking at file names. Files would get renamed like this:

01 01 01 file23.txt >becomes> file23.txt
02 file717.txt >becomes> file717.txt
07 07 07 07 99 file.txt >becomes> 99 file.txt
file444.txt is unchanged

I was thinking that maybe this could be done using find piped into xargs, but I am not really sure how to do this.

Any feedback on this would be appreciated very much.
# 2  
Old 01-14-2010
Code:
$ echo "01 01 01 file23.txt" | sed "s/\([^ ]* \)\1\{0,\}//"
file23.txt
$ echo "07 07 07 07 99 file.txt" | sed "s/\([^ ]* \)\1\{0,\}//"
99 file.txt
$  echo file444.txt | sed "s/\([^ ]* \)\1\{0,\}//"
file444.txt
$ echo "02 file717.txt" | sed "s/\([^ ]* \)\1\{0,\}//"
file717.txt

# 3  
Old 01-15-2010
With thanks to anbu23 for the sed:

Code:
find . -type f |
while read -r f; do
    b1=${f##*/}
    b2=`echo "$b1" | sed 's/^\([0-9][0-9] \)\1\{0,\}//'`
    [ "$b1" != "$b2" ] && mv "$f" "${f%/*}/$b2"
done

If you also want to rename directories, the find invocation will need to be modified (to drop the file type check and to specify depth first traversal).

Cheers,
alister
# 4  
Old 01-15-2010
Thank you both for the responses. The sed syntax from anbu23 is fantastic. Also, I ran the script that alister posted here on a bunch of test files and it works exactly as hoped. I will study this command structure to help me improve my scripting. Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

How to remove. (dot) if found in the beginning of file name while doing wget (download)?

Dear All, How to remove. (dot) if found in the beginning of file name while doing wget (download)? I am facing problem while re-sizing the image by using ImageMagick. Two dots in the file name are causing problem. ImageMagick is skipping such image with a dot . in the beginning, like ... (1 Reply)
Discussion started by: Praveen Pandit
1 Replies

3. Shell Programming and Scripting

How to grab a block of data in a file with repeating pattern?

I need to send email to receipient in each block of data in a file which has the sender address under TO and just send that block of data where it ends as COMPANY. I tried to work this out by getting line numbers of the string HELLO but unable to grab the next block of data to send the next... (5 Replies)
Discussion started by: loggedout
5 Replies

4. Shell Programming and Scripting

Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format: I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one. Any help would be greatly appreciated. (10 Replies)
Discussion started by: nwalsh88
10 Replies

5. Shell Programming and Scripting

How to remove a semi-repeating character in position 1 of a file

My file is in a good column format but several lines in the file begin with a zero. I'm in KSH and looking for a command to remove this zero and keep the text next to it. I don't want any of the zeros in the other columns removed. Below is a snip from the file... all I need to do is remove that... (2 Replies)
Discussion started by: right_coaster
2 Replies

6. UNIX for Dummies Questions & Answers

Using sed command to remove multiple instances of repeating headers in one file?

Hi, I have catenated multiple output files (from a monte carlo run) into one big output file. Each individual file has it's own two line header. So when I catenate, there are multiple two line headers (of the same wording) within the big file. How do I use the sed command to search for the... (1 Reply)
Discussion started by: rebazon
1 Replies

7. Shell Programming and Scripting

How to find pattern in file names?

I have some files, those are abbreviated (ed,ea, and bi) company_ed_20100719.txt company_ea_20100719.txt company_bi_20100719.txt I would like to rename these files by replacing ed with EmployeeDetails ea with EmployeeAddress bi with BankInfomration as company_... (3 Replies)
Discussion started by: LinuxLearner
3 Replies

8. Shell Programming and Scripting

Command to remove numbers from beginning of txt file

Hello. I have the following issue: my txt file has the following format: train/dr4/fklc0/sx175.txt 0 80282 Severe myopia contributed to Ron's inferiority complex. train/dr4/fklc0/sx355.txt 0 42906 Dolphins are intelligent marine mammals. train/dr4/fklc0/sa2.txt With the... (1 Reply)
Discussion started by: li_bi
1 Replies

9. UNIX for Dummies Questions & Answers

Remove words beginning with a certain character from a file

Hi, how could you go about removing words that begin with a certain character. assuming that this character is '-' I currently have echo "-hello" | sed s/-/""/ which replaces the leading dash with nothing but I want to remove the whole word, even if there are multiple words beginning... (3 Replies)
Discussion started by: skinnygav
3 Replies

10. Shell Programming and Scripting

Can I split a 10GB file into 1 GB sizes using my repeating data pattern

I'm not a unix guy so excuses my ignorance... I'm the database ETL guy. I'm trying to be proactive and devise a plan B for a ETL process where I expect a file 10X larger than what I process daily for a recast job. The ETL may handle it but I just don't know. This file may need to be split... (3 Replies)
Discussion started by: john091
3 Replies
Login or Register to Ask a Question