LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to replace text in txt file using unix command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-replace-text-in-txt-file-using-unix-command-4175450046/)

lwang3rock 02-13-2013 04:33 PM

how to replace text in txt file using unix command
 
I have a thegeekstuff.txt file as the following
nstruction Guides
1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux-Unix
5. Productivity (Too many technologies to explore, not much time available)
# Additional FAQS
6. Windows- Sysadmin, reboot etc.

I want to replace Linux-Unix with "Linux Unix". But it didn't work.
lwang@california:~$ sed 's/Linux-Unix/Linux Unix/g' thegeekstuff.txt
nstruction Guides
1. Linux Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not much time available)
# Additional FAQS
I tried "sed 's/Linux-Unix/Linux\sUnix/g'", it didn't work either.

chrism01 02-13-2013 05:13 PM

Interesting, it worked for me...
What version of sed?
Code:

sed --version
GNU sed version 4.2.1

Is this file in Linux format or did you copy it from MSWin?

RaviTezu 02-14-2013 03:09 AM

Try this:
Quote:

$ sed 's/Linux-Unix/Linux\ Unix/g' thegeekstuff.txt
As the replacement string is having a space(between Linux & Unix words).. the string Linux-Unix will be replaced with Linux(First word in replacement string) only.
So you have to use escaping character(\) before the space in order to skip it.


Using perl:
Quote:

$ perl -pi -w -e "s/Linux-Unix/Linux\ Unix/g" thegeekstuff.txt

lwang3rock 02-14-2013 12:22 PM

Thanks for the replay.
lwang@california:~$ sed --version
GNU sed version 4.1.5

I tried the following, it looked right on the console print out, but the thegeekstuff.txt file wasn't changed.
lwang@california:~$ sed 's/Linux-Unix/Linux\ Unix/g' thegeekstuff.txt
truction Guides
1. Linux Unix Sysadmin, Linux Unix Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux Unix
5. Productivity (Too many technologies to explore, not much time available)
# Additional FAQS
6. Windows- Sysadmin, reboot etc.

so I did
1. "sed 's/Linux-Unix/Linux\ Unix/g' thegeekstuff.txt > newgeek", newgeek has what I wanted.
2. I added "-i" so that thegeekstuff.txt was changed in place.
"sed -i 's/Linux-Unix/Linux\ Unix/g"

$ perl -pi -w -e "s/Linux-Unix/Linux\ Unix/g" thegeekstuff.txt worked for me as well.


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