LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-14-2005, 03:19 PM   #1
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Rep: Reputation: 15
Replace text of unknown content with other text in file


I need a script that will replace text which I don't know ahead of time on a line that contains words that don't change ("foobar") and are unique to the file. Specifically, I want to make the line that reads:

Code:
          foobar = 123abc                          \

into:

          foobar = xyz456                          \
but I don't know the exact contents of "123abc" ahead of time.


I'll also need to do this with a block of text as well, replacing the block of text between the lines containing "foo" and "bar" with a block of text from a file. Ex:

foo
Text To
Be Replaced
bar

becomes:

foo
Replacement
Text
bar


but, again, I don't know the exact contents of "Text To Be Replaced" ahead of time.


Thanks.

Last edited by brian0918; 07-14-2005 at 03:45 PM.
 
Old 07-14-2005, 04:07 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Have a look at "sed".
Is this homework?
 
Old 07-14-2005, 04:09 PM   #3
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Uhh... no. I'm done with the concept of homework. I've been trying to work with sed but no luck.
 
Old 07-14-2005, 04:14 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Your second task will require awk, as sed is line-based only.

The first one, you could always just do:
Code:
sed 's/foobar = .*/foobar = xyz456/' FILE > NEW_FILE
There are probably nicer ways to handle this, but that should do it for you, as a quick-and-dirty technique.
 
Old 07-14-2005, 04:15 PM   #5
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Actually, that doesn't work, because it deletes the \ from the end of the line.

Last edited by brian0918; 07-14-2005 at 04:27 PM.
 
Old 07-14-2005, 04:26 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
change the single quotes to double quotes and insert the variable.

Code:
sed "s/foobar = .*/foobar = ${I_AM_A_SHELL_VARIABLE}/" FILE > NEW_FILE
The braces, though not strictly required, are good for readability there. They're also especially good if you want this to work
Code:
$ export VAR="test"
$ echo $VARing

$ echo ${VAR}ing
testing
 
Old 07-14-2005, 04:27 PM   #7
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Actually, that doesn't work, because it deletes the \ from the end of the line.
 
Old 07-14-2005, 04:32 PM   #8
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
This one should be obvious.
Code:
sed "s/foobar = .*/foobar = ${I_AM_A_SHELL_VARIABLE} \\/" FILE > NEW_FILE
 
Old 07-14-2005, 04:43 PM   #9
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
I got it working by replacing \\ with \\\

Last edited by brian0918; 07-14-2005 at 04:46 PM.
 
Old 07-14-2005, 04:47 PM   #10
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Oops, sorry. I guess the shell did a bit of interpretation due to the double quotes.
 
Old 07-14-2005, 04:58 PM   #11
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
The most complicated thing I've done with awk is printing a specific column. I have no clue how to replace blocks of text in that way.
 
Old 07-14-2005, 05:22 PM   #12
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
I think I got it to work with sed.
 
Old 07-14-2005, 05:25 PM   #13
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Even the bigger block? How'd you do that?
 
Old 07-14-2005, 05:29 PM   #14
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Found this on linuxjournal:

Quote:
Now, we can combine these last two commands to replace a block of text in a file with the contents of another file. We do it like this:

sed -e '/START/r bar' -e '/START/,/END/d' foo

This finds a line containing START, deletes through to a line containing END, then reads in the contents of the file bar. Because the r command doesn't read in the file until the next input line is read, the d command is executed before the new text is read in, so the d command doesn't delete the new text, as one might expect, looking at this command. The -e option tells sed that the next argument is a command, rather than an input file. Although it is optional when there is only one command, if we have multiple commands, they must each be preceded with -e.

http://www.linuxjournal.com/article/1224
 
Old 07-14-2005, 05:35 PM   #15
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Awk solution

And here is an awk solution:

Code:
 awk -- " /foo$/ {p=1;}; /bar$/ {e=1;}; /.*/ { if(p==1) {print \"somethingelse\nsecond row\"; b=1; p=0; }; if(b!=1) { print; }; if(e==1) { e=0; b=0; }}; "
Not as clear as sed though, but that comment about
Quote:
so the d command doesn't delete the new text, as one might expect, looking at this command
tells me maybe I don't like very much sed for a good reason. I admit that the above command is not very clear, but for someone used with flex, it's not that bad.(and it does what one might expect )
 
  


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
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 03:22 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM
best method to replace data in a text file? gsbarry Linux - General 5 04-15-2003 08:21 AM


All times are GMT -5. The time now is 12:27 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration