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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-18-2012, 02:29 AM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 6
Rep: 
|
Shell script to find a filename in a folder & change text within the file
Hi there.
I don't have much experience or any training with shell scripts so please forgive any stupid questions or mistakes.
I have a folder that contains different files. I need a script that will find only files that have the word "CYCLIC" in the filename and for those files only, find and replace any text within the file that matches "CTRRET" with "CYCLIC_CTRRET".
Here is what I have come up with after taking bits and pieces from various forum discussions, but it doesn't work. I'm hoping someone could show me where I'm going wrong.
*******************************************
#!/bin/bash
basePath="/cygdrive/$DOC1_DRIVE/conret/"
for f in $basePath
do
if [ $fn = *CYCLIC* ]
then
sed -i "s/_CTRRET/_CYCLIC_CTRRET/g" "$f"
fi
done
*******************************************
I'd appreciate any help anyone could provide.
Thank you.
|
|
|
05-18-2012, 02:44 AM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Basically in your script the offending line is
Code:
if [ $fn = *CYCLIC* ]
where fn is not the loop variable and the comparison does not make sense. Not to mention the loop cycles only once over the value of the basePath variable. My suggestion is to keep it simple and use a one-line stream of commands, e.g.
Code:
grep -Zrl CYCLIC $basePath | xargs -0 sed -i 's/_CTRRET/_CYCLIC_CTRRET/g'
Feel free to ask if something is not clear about this command.
|
|
1 members found this post helpful.
|
05-18-2012, 03:57 AM
|
#3
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,274
|
why don't you execute
Code:
sed -i 's/_CTRRET/_CYCLIC_CTRRET/g' $basePath/*CYCLIC*
it will send an error message if there was no such file, otherwise it works
_______________________
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
|
|
|
05-18-2012, 04:23 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by pan64
why don't you execute
Code:
sed -i 's/_CTRRET/_CYCLIC_CTRRET/g' $basePath/*CYCLIC*
it will send an error message if there was no such file, otherwise it works
|
This would work if the files were all in the same directory, but it's not recursive. Anyway, you make me notice that the word CYCLIC must be in the filename, not inside the file. Therefore instead of grep we can use find to search recursively inside the base directory:
Code:
find $basePath -name \*CYCLIC\* -exec echo sed -i 's/_CTRRET/_CYCLIC_CTRRET/g' {} \;
@Simon1984: please notice the echo command after -exec: it is for testing purposes. It will display the sed commands without actually execute them, so that you can check the result to see if it works as expected.
|
|
|
05-18-2012, 04:35 AM
|
#5
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,274
|
Quote:
Originally Posted by colucix
This would work if the files were all in the same directory, but it's not recursive.
|
That's why:
Quote:
Originally Posted by Simon1984
Hi there.
...
I have a folder that contains different files.
|
Also based on the original "script" there are no subdirs in this case. But I'm not really sure
|
|
|
05-18-2012, 06:14 AM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by pan64
Also based on the original "script" there are no subdirs in this case. But I'm not really sure
|
Let's see. The OP should clarify (hopefully).
|
|
|
05-18-2012, 07:09 AM
|
#7
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep: 
|
thanks for the suggestions guys
All my files are in the same folder so I tried the following code and it works fine
sed -i 's/_CTRRET/_CYCLIC_CTRRET/g' $basePath/*CYCLIC*
I appreciate all the suggestions and feedback as it's helped me to understand a bit more about how the code works.
Thanks again!
|
|
|
All times are GMT -5. The time now is 05:39 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|