LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-13-2013, 04:33 PM   #1
lwang3rock
LQ Newbie
 
Registered: Nov 2012
Posts: 20

Rep: Reputation: Disabled
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.
 
Old 02-13-2013, 05:13 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
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?
 
Old 02-14-2013, 03:09 AM   #3
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
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

Last edited by RaviTezu; 02-14-2013 at 03:17 AM.
 
2 members found this post helpful.
Old 02-14-2013, 12:22 PM   #4
lwang3rock
LQ Newbie
 
Registered: Nov 2012
Posts: 20

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


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
sed command to replace line in text file gengwei89 Linux - Newbie 6 11-06-2011 08:10 AM
[SOLVED] command to find/replace in a text file? sneakyimp Linux - Newbie 17 02-15-2011 12:30 AM
Linux command to find and replace string within text file chips11 Linux - Newbie 5 11-24-2008 02:25 PM
Need command to search and replace text in file acascianelli AIX 12 04-11-2007 08:16 PM
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 03:07 PM


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