LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-18-2012, 03:29 AM   #1
Simon1984
LQ Newbie
 
Registered: May 2012
Posts: 6

Rep: Reputation: Disabled
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.
 
Old 05-18-2012, 03:44 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
Old 05-18-2012, 04:57 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,797

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
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.
 
Old 05-18-2012, 05:23 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by pan64 View Post
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.
 
Old 05-18-2012, 05:35 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,797

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
Quote:
Originally Posted by colucix View Post
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
 
Old 05-18-2012, 07:14 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by pan64 View Post
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).
 
Old 05-18-2012, 08:09 AM   #7
Simon1984
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using shell script how to switch user and change folder sureshpanchanathan Linux - General 1 02-10-2012 01:43 AM
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 02:38 PM
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 10:29 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
shell script + change shell && continue darkRoom Programming 6 02-25-2005 03:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration