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 11-24-2008, 07:21 AM   #1
chips11
LQ Newbie
 
Registered: Nov 2008
Posts: 13

Rep: Reputation: 0
Linux command to find and replace string within text file


I want to run a .sh script that will find all instances of gggggg in a single text file, and replace it with hhhhhhh.

I have been looking online and am not sure if I need to use grep or sed command and when I use the said command how to format it in my shell script.

I was trying to use
sed 's/ggggggg/hhhhhhh/g' public/usr/folder/myfile.txt

But I think you cant use the path using sed, so within my sh script, can how do I get it to run se within the directory I want, so that I can then simply put the file name, I thought maybe a cd command.

Maybe I should be using grep though.
 
Old 11-24-2008, 07:32 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
But I think you cant use the path using sed
Why do you say that? You can put anything you want in the SED syntax. Do mean path in the "s" command, or the path in the filename? (In this context, I suspect the latter)

Note that--regardless of where used--pathnames need to be in one of two forms:
1. Relative to the current position
2. Absolute, starting with "/"


Quote:
Maybe I should be using grep though.
GREP doesn't do find and replace....

I would go to http://tldp.org and get the Bash Guide for Beginners. Also, some excellent tutorials here: http://www.grymoire.com/Unix/
 
Old 11-24-2008, 07:36 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
Quote:
Originally Posted by chips11 View Post
But I think you cant use the path using sed
Why not? sed accepts file names with or without a relative or absolute path, as all the linux commands which accept a file name as argument. Otherwise you can always use the cd command, but it depends only on what is more comfortable for you.

Moreover, if you want to edit the file in place, use the -i option of sed (test it on a dummy copy of the same file before actually do).

Edit: too late....
 
Old 11-24-2008, 08:13 AM   #4
chips11
LQ Newbie
 
Registered: Nov 2008
Posts: 13

Original Poster
Rep: Reputation: 0
It was because it was saying something about the path in the error message I was receiving that I said about the path, message from the server, but I probably misunderstood it..

I added the -i and it is now working fine

sed -i 's/gggg/hhhhhhhhhhhhh/g' /path/to/file.txt

Thanks ever so much, really appreciate it.
 
Old 11-24-2008, 11:55 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
We like happy endings---thanks for the follow-up
 
Old 11-24-2008, 02:25 PM   #6
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,117
Blog Entries: 2

Rep: Reputation: 113Reputation: 113
rpl is a great command for doing this, it even supports doing it recursively
 
  


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
How to replace a string in a text file jpan Linux - General 3 10-14-2012 06:17 PM
How to replace string containing / in a text file tikit Linux - Newbie 4 09-05-2008 08:48 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM


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

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