LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how do i add a line to a file? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-add-a-line-to-a-file-924437/)

Beandip408 01-18-2012 11:24 AM

how do i add a line to a file?
 
i want to write a script to be able to insert a line like this to the file vim /etc/pam.d/system-auth

auth requisite pam_succeed_if.so user ingroup linuxgroup debug


i want that inserted into the that file right under the line:
auth sufficient pam_unix.so nullok try_first_pass



i also want to append this line on that same file:
session optional pam_mkhomedir.so

to look like this:
session optional pam_mkhomedir.so umask=0077



also how do i add a file string at the end of another file?

grail 01-18-2012 12:03 PM

Try having a look at sed as it would seem to do what you require. Here is a good tute to get you started.

business_kid 01-18-2012 12:11 PM

For the end:

cat >> your_file << eof

The system will return a '>'
> Add stuff here
> on as many lines as you like
> and at the end, type
>eof

In the middle it gets messy.

splante 01-18-2012 12:12 PM

Here's an example of adding a line after a particular line:

splante@guinness:/tmp/sed/> cat test
aaaaa
bbbbb
ccccc
ddddd
eeeee

splante@guinness:/tmp/sed/> sed '/ccccc/a zzzzz' test
aaaaa
bbbbb
ccccc
zzzzz
ddddd
eeeee

You could have redirected this to a temp file and then copied it back over the original file.

Cedrik 01-18-2012 12:21 PM

Quote:

Originally Posted by splante (Post 4577963)

You could have redirected this to a temp file and then copied it back over the original file.

or use -i option (edit in place)

Beandip408 01-18-2012 12:24 PM

Quote:

Originally Posted by splante (Post 4577963)
Here's an example of adding a line after a particular line:

splante@guinness:/tmp/sed/> cat test
aaaaa
bbbbb
ccccc
ddddd
eeeee

splante@guinness:/tmp/sed/> sed '/ccccc/a zzzzz' test
aaaaa
bbbbb
ccccc
zzzzz
ddddd
eeeee

You could have redirected this to a temp file and then copied it back over the original file.

do i have to copy the entire line where you put ccccc? like this:
sed -i '/auth sufficient pam_unix.so nullok try_first_pass/a auth requisite pam_succeed_if.so user ingroup lxgroup debug' /etc/pam.d/system-auth

splante 01-18-2012 01:08 PM

The '/ccccc' part is saying to search for 'ccccc'. It would find that anywhere in the line. You can use ^ for the start of the line and $ for the end of the line. If you wanted to match 'ccccc' exactly, you could say '/^ccccc$'. You can also include regular expressions. Note these examples:

splante@guinness:/tmp/sed/> cat test
aaaaa
bbbbb
ccccccc
ccccc
ddddd
eeeee
splante@guinness:/tmp/sed/> sed '/ccccc/a ZZZZZ/' test
aaaaa
bbbbb
ccccccc
ZZZZZ/
ccccc
ZZZZZ/
ddddd
eeeee
splante@guinness:/tmp/sed/> sed '/^ccccc$/a ZZZZZ' test
aaaaa
bbbbb
ccccccc
ccccc
ZZZZZ
ddddd
eeeee
splante@guinness:/tmp/sed/> sed '/^[ad]/a ZZZZZ' test
aaaaa
ZZZZZ
bbbbb
ccccccc
ccccc
ddddd
ZZZZZ
eeeee

You have a lot of flexibility depending on how broad or exact you want to match. Check out the sed man page for more details.

Beandip408 01-18-2012 02:37 PM

thanks so much for all of your help. its been very useful. just 1 more question (hopefully).


is it possible to put a variable in one of these lines?:
sed -i '/some text here/a text before variable $DGROUP text after variable' /etc/pam.d/system-auth

Beandip408 01-18-2012 03:27 PM

k i think i got it. i used " " around the entire line instead of ' '

David the H. 01-19-2012 09:38 AM

sed and awk are the two workhorse programs for manipulating file contents. It would do you good to learn at least the basics of both of them:

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

In addition, both of these programs, and many others, support and rely heavily on regular expressions, which is one of the most useful computer concepts you can learn.

Some regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html


All times are GMT -5. The time now is 07:06 AM.