How to find and rename of particular pattern in file.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find and rename of particular pattern in file.?
# 1  
Old 01-08-2014
How to find and rename of particular pattern in file.?

Hi Guys,

I have folder called /input/temp. Inside the folder I have lot of files. I need to find the file of pattern Article_????_test_?????????.txt and replace by format below.

Article_????_?????????.txt

Below is the one which I tried but it doesn't works can you please help us.

Thanks in Advance
Code:
echo "Please provide the file name Corresponding to DC..."
read file 
ls $HOME/*.txt | grep $file
if [ $? -eq 0 ]
find . $file '*_Test_*' -exec bash -c 'mv $0 ${0/_Test/ }' {} \;

---------- Post updated at 03:39 AM ---------- Previous update was at 03:17 AM ----------

I tried this as well. But it doesn't works.

Code:
if [ $? -eq 0 ]
find . $file -name "*.txt" -exec bash -c "mv {} \`echo {} | sed -e 's/[_TEST_]/_/g'\`" \;
then

I Got below error:

find: 0652-083 Cannot execute bash:: A file or directory in the path name does not exist.
find: 0652-083 Cannot execute bash:: A file or directory in the path name does not exist.

bash can't be executed in my platform. Can u please provide solution for this
# 2  
Old 01-08-2014
Try the below code,

Code:
 
#!/bin/sh
echo "Please provide the file name Corresponding to DC..."
cd $HOME
read file 
ls $HOME/*.txt | grep $file
mv $file Article_01082014.log
echo "The file had been successfully renamed.."


Cheers,
Adi
# 3  
Old 01-08-2014
I am not understanding the requirement of yours and the commands that you run.

Code:
echo "Please provide the file name Corresponding to DC..."
read file 
ls $HOME/*.txt | grep $file --> You are checking the existence of the file
if [ $? -eq 0 ]
find . $file -name "*.txt" -exec bash -c "mv {} \`echo {} | sed -e 's/[_TEST_]/_/g'\`" \; --> Are you trying to rename the file that you read above??

---------------------------------

I have folder called /input/temp. Inside the folder I have lot of files. I need to find the file of pattern Article_????_test_?????????.txt and replace by format below.

Article_????_?????????.txt

Code:
find /input/temp -type f -name "Article_????_test_?????????.txt" -exec sh -c 'mv $1 ${1/test/}' {} \;

This should do!!

Last edited by PikK45; 01-08-2014 at 05:17 AM..
# 4  
Old 01-08-2014
Unix shell script

Hi Pikk,

Thanks for your response. Am just trying to rename the file which I read above.

I tried the command which u posted. But it doesn't works.

Error: /u/vgomu/Article_TEST_123.txt: ${1/test/}: 0403-011 The specified substitution is not valid for this command.

Code:
 echo "Please provide the file name Corresponding to DC..."
read file
ls $HOME/*.txt | grep $file
find /u/vgomu -type f -name "$file" -exec sh -c 'mv $1 ${1/test/}' {} \;

# 5  
Old 01-08-2014
If you just want to rename the single file you read.. what is the need of using find here? Smilie

Code:
echo "Please provide the file name Corresponding to DC..."
read file
if [ -f $file ]; then
mv $file ${file/TEST/}  --> Make sure your file has TEST in its name
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename text file with a specific pattern in directory

I am trying to rename all text files in a directory that match a pattern. The current command below seems to be using the directory path in the name and since it already exists, will not do the rename. I am not sure what I am missing? Thank you :). Files to rename in... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Rename files to match file list pattern

Hi All, I have 100 folders with the first delimiter has a unique name i.e (123_hello and 575_hello) and each folder have atlist 1000 plus files with naming convention i.e (575_hello_1.iso ... 575_hello_1000.iso). 575_hello/575_hello_1.iso 575_hello/575_hello_2.iso 575_hello/575_hello_3.iso... (8 Replies)
Discussion started by: lxdorney
8 Replies

3. UNIX for Dummies Questions & Answers

Find and rename file recursively

Hi, I have a directory which contains multiple files with .txt extension, i want to rename all these file to .bak extension using find command, this is what i've tried, please help me to correct this : find /home/application/test -name '*.txt' -exec rename 's/txt/bak/' {} \; seems to... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

4. Shell Programming and Scripting

Find and rename part of a file

hi, Need your help. I need to write a script for below.. i have two files in directory /home/abc as below: Watch_20140203_abc.dat Watchnow_20140203_abc.dat I have to copy this file from /home/abc to /home01/home02 after that i have to rename the date part in above two files... (1 Reply)
Discussion started by: Vivekit82
1 Replies

5. Shell Programming and Scripting

Rename the file with specific pattern

Hello I am making a script where I need to rename the files but with different names.The file name could be change according to the product I made a logic but that is not working properly arr=$(echo a@b@c | tr "@" "\n") echo $arr output is a b c arry=$(echo d@e@f | tr "@" "\n") ... (4 Replies)
Discussion started by: anuragpgtgerman
4 Replies

6. Shell Programming and Scripting

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 (5 Replies)
Discussion started by: alfredo123
5 Replies

7. Shell Programming and Scripting

Problem with Find and rename file

I want to find a file say IIFT and check its size is zero or not. If its zero then I have to rename anothe file say WWFT , which is in another folder to WWFT$Todaysdate. I tried below command: cd dir2 (*File WWFT is in dir2) find dir/ -type f -name 'IIFT*' -size 0 -exec mv WWFT... (3 Replies)
Discussion started by: ammbhhar
3 Replies

8. UNIX for Dummies Questions & Answers

Find and Replace then rename file

Hi, This is probably quite simple for an expert, but I keep getting confused about the best approach, grep, awk, sed. What I have is a range of files numbered 1 to 100. They go file1.txt file2.txt and so on In each file I need to find and replace a couple of items and rename add a... (5 Replies)
Discussion started by: chickenhouse
5 Replies

9. Shell Programming and Scripting

Find and Rename File using Terminal

I need help finding a file through terminal and then renaming it automatically. Here is what I have so far to find the file: cd /User/Applications find . */SourceM.app/banner.png | while read line; do mv "$line" banner-.png; done I want the script to rename the file "banner.png" to... (6 Replies)
Discussion started by: rbisconti97
6 Replies

10. Shell Programming and Scripting

Find and rename long file names (html)

Hi Guys, I need a help. I have 1130 zip files. Each one of them has files including 1 html file with long file name (includes special charactors, Alphabetic and numbers). I have copied all 1130 zip files to my linux system and extracted using below command. Find . -name "*.zip" -exec... (7 Replies)
Discussion started by: Rajmani
7 Replies
Login or Register to Ask a Question