Register
It is currently Wed Aug 20, 2014 4:43 am

Noob has a basic question about scripts and ed.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Sep 10, 2012 7:59 am   

Joined: Mon Sep 10, 2012 7:41 am
Posts: 2
I am a noob trying to use ed to replace a line of text in a file. I can not figure out the correct script syntax.

The following works in interactive mode:

Code:
ed file_name
g/search_string/
c
replacement_line
.
wq

How do I create a script that includes the above?

#search_string is unique text - just part of a line to be replaced.

Thanks in advance


Top
 Profile  
 PostPosted: Mon Sep 10, 2012 11:36 am   

Joined: Mon Sep 10, 2012 7:41 am
Posts: 2
The following works on the command line:

Code:
ed -s parameter_file <<< $'g/CURRENT_KEY\nc\nNEW_KEY        NEW_VALUE\n.\nw'

Seems like c switches ed from command mode to interactive mode until the . is reached. I thought there was a way to use c in command mode; I can't find it.

Now I need to figure out how to replace NEW_KEY NEW_VALUE with a string variable. I should replace CURRENT_KEY with a variable as well.


Top
 Profile  
 PostPosted: Tue Sep 11, 2012 7:22 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
PhillyG wrote:
I thought there was a way to use c in command mode; I can't find it.

What do you mean? What are you trying to achieve?
In input mode, your input is written to the file ; you can't use commands.


PhillyG wrote:
Now I need to figure out how to replace NEW_KEY NEW_VALUE with a string variable.
I should replace CURRENT_KEY with a variable as well.

Use printf(0).

Code:
printf '%s\n' "g/${current_key}/" c "$new_key"$'\t\t'"$new_value" . w | ed -s "$parameter_file"


PhillyG wrote:
How do I create a script that includes the above?

Code:
#!/bin/sh

...

> /dev/null ed -s "$parameter_file" << EOE
g/${current_key}/
c
$new_key         $new_value
.
w
EOE


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP