LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-18-2012, 10:24 AM   #1
Beandip408
LQ Newbie
 
Registered: Aug 2011
Posts: 20

Rep: Reputation: Disabled
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?
 
Old 01-18-2012, 11:03 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,483

Rep: Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864Reputation: 2864
Try having a look at sed as it would seem to do what you require. Here is a good tute to get you started.
 
Old 01-18-2012, 11:11 AM   #3
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 7,862

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
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.
 
Old 01-18-2012, 11:12 AM   #4
splante
LQ Newbie
 
Registered: Jun 2007
Posts: 5

Rep: Reputation: 0
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.
 
Old 01-18-2012, 11:21 AM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Quote:
Originally Posted by splante View Post

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)
 
1 members found this post helpful.
Old 01-18-2012, 11:24 AM   #6
Beandip408
LQ Newbie
 
Registered: Aug 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by splante View Post
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
 
Old 01-18-2012, 12:08 PM   #7
splante
LQ Newbie
 
Registered: Jun 2007
Posts: 5

Rep: Reputation: 0
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.
 
Old 01-18-2012, 01:37 PM   #8
Beandip408
LQ Newbie
 
Registered: Aug 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
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

Last edited by Beandip408; 01-18-2012 at 02:08 PM.
 
Old 01-18-2012, 02:27 PM   #9
Beandip408
LQ Newbie
 
Registered: Aug 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
k i think i got it. i used " " around the entire line instead of ' '
 
Old 01-19-2012, 08:38 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Add Line to a File ( 3rd line) NetRock Programming 6 04-07-2011 07:17 AM
How can I read a file line by line and add it to a loop in another file? astroumut Linux - Newbie 7 08-24-2009 04:37 AM
add more than one line to end of file Siljrath Linux - General 1 08-18-2009 11:01 PM
simple php script to add line/file to text file dnoy Programming 1 05-21-2008 05:08 PM
add word to each line of the file? ziggie216 Linux - General 4 04-25-2005 04:19 AM


All times are GMT -5. The time now is 03:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration