Re: Add to text file from command line via echo... possible to add to a particular line?[Scanned]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Chris Bradford wrote:
> Hi guys,
> 
> I'm making a setup script for my Linux boxes that re-install Openldap
> and some other bits and bobs. I need to add some text to /etc/pam.d/gdm
> for example, and it needs to be on a particular line in this file, not
> at the bottom.
> 
> For example using the command:
> 
> echo 'session   required   pam_mkhomedir.so skel=/etc/skel umask=0077'
>>> /etc/pam.d/gdm
> 
> Will only add this to the bottom of the file, can I get around this, it
> needs to be on the same position on all machines, ie line 8.
> 
> Cheers,
> 
> -Chris
> 
You might be better off using something like sed to find the
specific line you want to insert the new line after. That way, the
specific line number is not critical. For removal, you can search
for your specific line and delete it. Something like:

sed -i -e "/some line format/anew line" <file name>

The -i option means edit the existing file. (You can use i.bak to
create a backup file called <file name.>.bak.)

The /some line format/ searches for a line with "some line format"
in it. You can also use /^some line format$/ to require an exact match.

If you are sure you want to insert at a specific line, try something
like:

sed -i -e "8iNew Line" /etc/pam.d/gdm

This will insert "New Line" at line 8 of the file. You may find it
easier to put your sed commands in another file. This is especially
handy if you want to do more then one command. To do that, replace
-e "command string" with -f <sed script>.

Mikkel
-- 

  Do not meddle in the affairs of dragons,
for thou art crunchy and taste good with Ketchup!


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux