LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-14-2013, 03:25 AM   #1
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Rep: Reputation: Disabled
Question find and replace in each file if condition is true


I have a list of directories each having a file named .own now for every such file i want to check if it contains a string = "oldstring" replace it with "newstring". else do nothing. before replacing i also want 1 command to be run for that file, the command is
Code:
p4edit .own
I tried something like
Code:
find . -name .own
to give me all .own now it should grep "oldstring .own if true then sed s/oldstring/newstring. I am not sure about the pipe usage and how to check the condition
 
Old 02-14-2013, 03:31 AM   #2
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
1.
Quote:
perl -pi -w -e "s/oldstring/newstring/g" <filename>
----This will replace the string in a file.


2."cd"(cd <path-of-the-directory>) to the required directory, Then..

Quote:
perl -pi -w -e "s/oldstring/newstring/g" *
----This will replace the string in every file which is residing in the directory.


Note: you should be having write permissions on that file(s) to replace the string.

Last edited by RaviTezu; 02-14-2013 at 03:37 AM. Reason: Syntax error, Corrected it.
 
Old 02-14-2013, 05:05 AM   #3
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
Using the -execdir action, find executes the command from inside the directory where the file is placed:
Code:
find . -name .own -execdir bash -c 'if grep -q oldstring "{}"; then p4edit "{}"; sed -i "s/oldstring/newstring/g" "{}"; fi' \;
 
2 members found this post helpful.
Old 02-14-2013, 09:16 AM   #4
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks colucix, this doesnt seem to work maybe because
Quote:
echo $SHELL
/bin/sh
Is this a case that my shell is sh and we are trying to give bash, but even replacing bash with sh didnt help!?

Last edited by shridhar22; 02-14-2013 at 09:24 AM.
 
Old 02-14-2013, 10:00 AM   #5
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
I don't see anything bash-specific, hence it should work with /bin/sh as well. What OS are you running on? And what does "doesn't seem to work" mean? Any error message?
 
Old 02-14-2013, 10:28 AM   #6
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Arrow

Quote:
Originally Posted by colucix View Post
I don't see anything bash-specific, hence it should work with /bin/sh as well. What OS are you running on? And what does "doesn't seem to work" mean? Any error message?
Your reply did the trick, the problem was i had tried it on some wrong directory which didnt have the oldstring, and i wrote "seems" because i was a bit confused that why it didnt give any error if it did not work. Sorry for the lame mistake.

PS: With time i have started liking shell scripting (not exactly linking but rather enjoying )
 
Old 02-14-2013, 10:33 AM   #7
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
btw whats the significance of bash -c ? and why did you give {} in "" and not simple {} without ""
 
Old 02-14-2013, 10:54 AM   #8
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 shridhar22 View Post
btw whats the significance of bash -c ? and why did you give {} in "" and not simple {} without ""
Since the command has multiple statements separated by semi-colon, it would give an error if you write it directly after -exec or -execdir. Therefore it's better (if not mandatory) to put the whole command line inside single quotes and let a shell execute the command with the -c option.

Regarding the quotes around {} it's just an habit. They are mandatory if file names contain blank spaces (not true in this case, since you search for an exact file name without spaces).
 
  


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
[SOLVED] Bash script to replace line break with condition spart1985 Linux - Newbie 8 02-23-2012 09:43 AM
find command return true if a file was found catalint Programming 12 05-19-2011 06:52 AM
[SOLVED] command to find/replace in a text file? sneakyimp Linux - Newbie 17 02-15-2011 12:30 AM
replace several lines in a file with other lines in another file if condition yara Linux - General 12 10-27-2009 03:46 PM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM

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

All times are GMT -5. The time now is 04:28 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