[SOLVED] Shell script to find a filename in a folder & change text within the file
Linux - NewbieThis 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.
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.
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
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.
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.
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:
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.