You are not logged in.

#1 2009-03-27 14:03:00

net.reg
Member
Registered: 2009-03-26
Posts: 12

insert text with 'sed'

How can 'sed' (or any other line editor)  be used to insert text,
     like add 'hal' and 'fam' in DAEMONS in /etc/rc.conf
     or adding something to MODULES line?

i couldn't make out the meaning in 'man sed', hence looking for reverse way, ie, asking u to kindly give a short one line example.

Offline

#2 2009-03-27 14:09:43

nocentis
Member
From: pl_PL || Posnan
Registered: 2009-03-12
Posts: 23
Website

Re: insert text with 'sed'

MODULES=(fam hal) ?


Every day I make furniture:) Polecam meble na wymiar meble poznań & kuchnie na wymiar poznań.

Offline

#3 2009-03-27 14:18:15

byte
Member
From: Düsseldorf (DE)
Registered: 2006-05-01
Posts: 2,046

Re: insert text with 'sed'

sed -i '/^DAEMONS=/ s/)$/ fam)/' rc.conf

Adds fam at the end of DAEMONS.

You should have a look at http://www.grymoire.com/Unix/Sed.html (or other tutorials for sed and regular expressions)

Last edited by byte (2009-03-27 14:21:35)


1000

Offline

#4 2009-03-27 14:21:01

evr
Arch Linux f@h Team Member
Registered: 2009-01-23
Posts: 554

Re: insert text with 'sed'

is there a reason you are wanting to use sed instead of editing the file by hand?

Offline

#5 2009-03-27 16:23:14

thisperishedmin
Member
Registered: 2008-11-04
Posts: 164

Re: insert text with 'sed'

evr wrote:

is there a reason you are wanting to use sed instead of editing the file by hand?

I'm gonna have to pose the same question...SED can be very unforgiving...as I've found out the hard way tongue

furthermore...I typically use SED for more of a "search and replace" strategy than simply adding to the end of a file. For example...replacing "line that starts and reads like this" to something like "line that used to start one way, but now reads like this".

If youre simply appending data to the end of a file....cat may be a better option for you.

This way you could add "line that used to start one way, but now reads like this" directly to the end of the file using a command something like:
cat original >> new

even better for testing purposes would be:
cat original text_to_add > new.txt

this way would take the original file, plus the file with the text you wish to add, and create a whole new file called new.txt.  then you could verify its what you want and simply move / rename it as appropriate.

for that - ill hae to advise checking out the cat command moreso than sed.

nonetheless I may be missing the mark totally on what youre trying to accomplish...but in the end I think editing it by hand is the safest / easiest / smartest way to do things.

If youre looking to learn sed it would be much wiser to practice on dummy files big_smile

good luck!

Offline

#6 2009-03-28 16:21:15

net.reg
Member
Registered: 2009-03-26
Posts: 12

Re: insert text with 'sed'

Thanks indeed for your replies.

why i wanted it with sed? ---> to kill Bug no. 509 in my frontal lobe that urged me do do it via command line ; to make a small script that accomplishes a few small installation editings.

Offline

#7 2009-03-28 16:27:49

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: insert text with 'sed'

Next time, search Google or check the man page.

Last edited by haxit (2009-03-28 16:27:59)


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

#8 2009-03-28 21:08:36

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: insert text with 'sed'

haxit wrote:

Next time, search Google or check the man page.

A bit harsh, isn't it?
But, true, try to understand how to help yourself. info sed is a good source of documentation. Also, there are quite some outstanding sed tutorials available in the web.

This applies to similar problems as well of course.
If you try for yourself you will always find a helpful hand once you got stuck.

Last edited by bernarcher (2009-03-28 21:11:25)


To know or not to know ...
... the questions remain forever.

Offline

#9 2009-03-29 15:36:56

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: insert text with 'sed'

bernarcher wrote:
haxit wrote:

Next time, search Google or check the man page.

A bit harsh, isn't it?
But, true, try to understand how to help yourself. info sed is a good source of documentation. Also, there are quite some outstanding sed tutorials available in the web.

This applies to similar problems as well of course.
If you try for yourself you will always find a helpful hand once you got stuck.

I didn't mean it in a harsh way, it was a friendly suggestion. Maybe I should have put a lol or mad or cool or big_smile or smile after it.


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

Board footer

Powered by FluxBB