Grep and rename the filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and rename the filename
# 8  
Old 04-30-2009
I agree to this. We should probably have a counter appended to the new filenames.

Code:
mv "$file" "COM_$i_$(date + "%Y%m%d").txt"

and increment i for each file. just a thought but depends on the exact requirement.
# 9  
Old 04-30-2009
Hi all,

Let me explain ya..

In folder A, there are several file with different name.
Example :

PTWO121.txt
PTWO122.txt
PONE145.txt
PTWO151.txt
PONE181.txt
PTWO191.txt

I only want to grep the file that the name start with PTWO and rename it to COM and current date.

Desired output :

COM20090412.txt
COM20090412.txt
PONE145.txt
COM20090412.txt
PONE181.txt
COM20090412.txt

Thanks for your help~
# 10  
Old 04-30-2009
Quote:
Originally Posted by badbunny9316
Desired output :

COM20090412.txt
COM20090412.txt
PONE145.txt
COM20090412.txt
PONE181.txt
COM20090412.txt

Thanks for your help~
It's not possible to have 4 files with the same name in a directory.
# 11  
Old 04-30-2009
Quote:
Originally Posted by badbunny9316
Hi all,

Let me explain ya..

In folder A, there are several file with different name.
Example :

PTWO121.txt
PTWO122.txt
PONE145.txt
PTWO151.txt
PONE181.txt
PTWO191.txt

I only want to grep the file that the name start with PTWO and rename it to COM and current date.

Desired output :

COM20090412.txt
COM20090412.txt
PONE145.txt
COM20090412.txt
PONE181.txt
COM20090412.txt

Thanks for your help~
Two different files can't have the same name. It will overwrite the exiting one when you try to move another one with the same name. That is why I am suggesting you have filenames like below. If that works out for you, you can use my old code with the i.
COM_1_20090412.txt
COM_2_20090412.txt
# 12  
Old 04-30-2009
Hi all,

Thanks Franklin52 for the reminder.

This is my desired output :

COM20090412_1.txt
COM20090412_2.txt
PONE145.txt
COM20090412_3.txt
PONE181.txt
COM20090412_4.txt

To devtakh : The script that you give hit an error.

I still can't solve it..

Thanks for your help~
# 13  
Old 04-30-2009
What is the script you are using and what is the error it is giving.
# 14  
Old 04-30-2009
Here is the code for your issue.

=========code================

count=`ls PTWO* | wc -l`
old=PTWO
day=`date +%Y%m%d`
new=COM$day

while [ "$count" -gt 0 ]
do
count=`ls PTWO* | wc -l`
file=`ls PTWO* | tail -1`
newfile=`echo "$file" | sed "s|"$old"|"$new"|g"`
mv $file $newfile > /dev/null

done

echo " RENAME DONE"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename first N numeric strings in filename

Hello, I have some video files containing numbers and characters . To tell the problem shortly, due to a limitation, I am unable create a playlist regularly changing on a daily basis with the command shuffle....So I decided to rename filenames, just a replacement of first five numbers randomly.... (10 Replies)
Discussion started by: baris35
10 Replies

2. Shell Programming and Scripting

Shell script to get one to one map and rename the filename

I have 2 files sorted by numerically. I need help with shell script to read these 2 files and do a 1:1 mapping and rename the filenames with the mapped case#; For example: cat case.txt 10_80 10_90 cat files.txt A BCD_x 1.pdf A BCD_x 2.pdf ls pdf_dir A BCD_x 1.pdf A BCD_x 2.pdf ... (2 Replies)
Discussion started by: iaav
2 Replies

3. Shell Programming and Scripting

Filename rename with characters of file

Hi, I need a bit of help. I've used awk to get the first 7 characters of a file - awk '{print substr($0,0,7)}' test.csv How do I now take this variable to rename test.csv to variable.csv ? Any help or advice would be greatly appreciated! (2 Replies)
Discussion started by: sianm
2 Replies

4. Shell Programming and Scripting

Rename all files (filename with spaces) to different extension

Hi, I have files with filenames as below. SGM Daily Sales Email-en-us-05312012.xlwa I want to rename it in .xls. I am writing a script to change this, as there can be multiple files in subfolders. I have the following script. #!/bin/ksh for oldfile in $(find... (1 Reply)
Discussion started by: mac4rfree
1 Replies

5. Shell Programming and Scripting

wget same filename from subdirectories and rename or concat

I would like to wget a file "index.html" from a site which is lies in different subdirectories and is different in size. The index.html shall be concatenated or renamed to index01.html index02.html .... I would like to store this file in just one directory and use the option -nd. Though i can't get... (0 Replies)
Discussion started by: sdf
0 Replies

6. Shell Programming and Scripting

Rename FileName in the Directory

In the Directory all the Files are following format. Filename_yyyymmdd_numbers.txt eg. file_name_20120106_015802.txt . I want to write the Shell script to rename all the file to file_name.txt.in the directory. Thanks Mani (5 Replies)
Discussion started by: gavemani
5 Replies

7. UNIX for Dummies Questions & Answers

rename filename

Hi, I am pretty new to this. I have a condition where in I want to replace all files within a folder. All filenames with character "abc" would need to replaced with "xyz". eg: helloabcworld-->helloxyzworld helloworld-->helloworld ... ... Thanks in advance. (6 Replies)
Discussion started by: sakets_2000
6 Replies

8. Shell Programming and Scripting

How do I rename a filename in a directory?

Hi, I've got a large to task to do, which I've broken into three section. I'm just stuck on one of the sections. I have to change the end of a filename from .txt to .doc in a directory. So if I have a directory called "folder1" and two files contained in it called "file1.txt" and "file2.txt",... (7 Replies)
Discussion started by: TeddyP
7 Replies

9. Shell Programming and Scripting

how to rename all files that have a certain text in the filename using tcsh shell

Hello~ I'm on AIX version 5 and I believe I have the tcsh shell environment to play in. Can you guys help me with a solution to rename all files that have "eclp" in the filename to "ecl" ? I basically want to rename the files and strip the "p" out. i.e. original filenames: ... (3 Replies)
Discussion started by: in2vtec
3 Replies

10. Shell Programming and Scripting

rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named file.46634.txt budget.75346.pdf etc i want to remove the number but retain the extension. it is always a 5 digit. thanks. (6 Replies)
Discussion started by: jason7
6 Replies
Login or Register to Ask a Question