LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Command to find in each file and replace one or two words (https://www.linuxquestions.org/questions/linux-newbie-8/command-to-find-in-each-file-and-replace-one-or-two-words-914285/)

Sha_unix 11-18-2011 02:27 PM

Command to find in each file and replace one or two words
 
Hi
i have few files called

asdf.sh
qwer.sh
hxhc.sh
.
.
.etc

cat asdf.sh
PROGHOME=/home/progress
find / -name /user/folder/

now i want to replace /home to /mine and /folder to /fold in all *.sh files in single shot as i dont want to get into each file and modify... pleaes let me know if any command such.

TB0ne 11-18-2011 02:59 PM

Quote:

Originally Posted by Sha_unix (Post 4527802)
Hi
i have few files called

asdf.sh
qwer.sh
hxhc.sh
.
.
.etc

cat asdf.sh
PROGHOME=/home/progress
find / -name /user/folder/

now i want to replace /home to /mine and /folder to /fold in all *.sh files in single shot as i dont want to get into each file and modify... pleaes let me know if any command such.

Very similar to your other thread:
http://www.linuxquestions.org/questi...e-shot-913500/

Replace the sed delete command in that script with a substitute command. Try that?

Sha_unix 11-18-2011 03:08 PM

But my replacement has / (forwardslash) those slashes are making me trouble. i used \(backslash) but may be some where i am doing wrong it seems, not able to get desired output.

Code:

sed '\/emc/reale/apps/pseudo/rsdm.pf /s\//emc_str/reale/apps/pseudo/rsdm_str.pf /g' vendors2.sh usersecg.sh

TB0ne 11-19-2011 04:54 PM

Quote:

Originally Posted by Sha_unix (Post 4527824)
But my replacement has / (forwardslash) those slashes are making me trouble. i used \(backslash) but may be some where i am doing wrong it seems, not able to get desired output.

Code:

sed '\/emc/reale/apps/pseudo/rsdm.pf /s\//emc_str/reale/apps/pseudo/rsdm_str.pf /g' vendors2.sh usersecg.sh

You have to put a backslash in front of EVERY forwardslash, not just one.

jthill 11-19-2011 06:18 PM

sed allows arbitrary regex delimiters. Your '\/' told it to use forward slash as a delimiter. The man page doesn't point out (but the info node does) that the s command uses the first following character as a delimiter: sed s,a,b,g is equivalent to tr a b.

Sha_unix 11-22-2011 01:30 PM

i tried using every time backslash didnt workout :(

code:
sed '\/emc/reale\/apps\/pseudo\/rsdm.pf /s\/\/emc_str\/reale\/apps\/pseudo\/rsdm_str.pf /g' vendors2.sh usersecg.sh

[/code]

i dont see anything on right like "Add to Reputation" button, on the left of the post.

TB0ne 11-22-2011 01:36 PM

[QUOTE=Sha_unix;4530866]i tried using every time backslash didnt workout :(
Code:

sed '\/emc/reale\/apps\/pseudo\/rsdm.pf /s\/\/emc_str\/reale\/apps\/pseudo\/rsdm_str.pf /g' vendors2.sh usersecg.sh
...because you're not calling SED correctly for a substitution. Quick Google search for "sed substitution examples", yields:
http://www.thegeekstuff.com/2009/10/...tion-examples/

You're not telling it to substitute at the beginning.
Code:

sed 's/\/emc\/reale\/apps\/pseudo\/rsdm.pf/\/emc_str\/reale\/apps\/pseudo\/rsdm_str.pf/g' <ONE filename>
See your previous thread that I referenced here. Sed will still only take one file at a time, unless you script it, which is what your last thread had already written.
Quote:

i dont see anything on right like "Add to Reputation" button, on the left of the post.
The little "scales" icon...when hovered over, tells you that's what it is.

Sha_unix 11-22-2011 01:44 PM

Thank you. its working now. and i posted this reply as helpful, is that what it adds to reputation i guess. :)


All times are GMT -5. The time now is 06:24 PM.