You are not logged in.

#1 2009-01-23 10:01:08

fiod
Member
Registered: 2007-04-02
Posts: 205

Howto add a string to multiple files from the shell

Hi,

I want to add a line of text to several files.
I can locate the files I want to add the text line to using this command: grep -lr "/bin/sh" .

This generates the list of files I need to modify.
Now, I want to add a string - LANG=he_IL - to all those files, AFTER the line #!/bin/sh (this line always comes first in every file).

Can anyone suggest a simple bash script to do so?

Thanks a lot,
Fiod

Offline

#2 2009-01-23 11:33:36

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: Howto add a string to multiple files from the shell

Read up on sed.
End of line sign is '$'. So that would become:

sed 's/$/LANG=he_IL/' files

/edit
Oh and to skip the first line there's a rule too, but you could also use something like:

sed '\@#\!/bin/sh@!s@$@LANG=he_IL@' files

please first test this code on an example in case I forgot something!

Last edited by ibendiben (2009-01-23 11:49:13)

Offline

#3 2009-01-23 13:00:48

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Howto add a string to multiple files from the shell

sed -i '2i LANG=he_IL' $(grep -lr .)

Offline

#4 2009-01-23 14:35:40

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: Howto add a string to multiple files from the shell

Procyon wrote:
sed -i '2i LANG=he_IL' $(grep -lr .)

Worked like a charm!

Thanks..

Offline

Board footer

Powered by FluxBB