LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-02-2011, 08:17 PM   #1
chinhth2010
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
replace string by another string in bash shell


Dear user
I want to write a shell.sh to replace a tring by other string in one file.
example: in my convert.gs file has a string as bellow
'set z 1'
Now I want to find this line and replace into 'set z 2'
Please anyone tell me how to do
Thank you
 
Old 08-02-2011, 08:19 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by chinhth2010 View Post
Dear user
I want to write a shell.sh to replace a tring by other string in one file.
example: in my convert.gs file has a string as bellow
'set z 1'
Now I want to find this line and replace into 'set z 2'
Please anyone tell me how to do
Thank you
Please have a look at the 'sed' command.
Here's a good tutorial:
http://www.grymoire.com/Unix/Sed.html
 
Old 08-02-2011, 10:40 PM   #3
lithanus
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by chinhth2010 View Post
Dear user
I want to write a shell.sh to replace a tring by other string in one file.
example: in my convert.gs file has a string as bellow
'set z 1'
Now I want to find this line and replace into 'set z 2'
Please anyone tell me how to do
Thank you
As sycamorex states, sed is what you are looking for. In this case, it would be
Code:
cat convert.gs | sed -e 's/set z 1/set z 2/g' > new-convert.gs
Jay
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
String replace in shell script jnreddy Programming 3 05-28-2011 02:07 AM
string replace in bash script xombboxer Linux - Newbie 11 05-02-2011 03:23 AM
[SOLVED] replace string with empty string of dir path ted_chou12 Linux - Newbie 3 04-02-2011 06:43 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
Replace string in Shell scripting anandjk1 Programming 2 10-19-2007 08:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:46 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
Open Source Consulting | Domain Registration