LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2010, 12:37 AM   #1
Glenn D.
Member
 
Registered: May 2009
Location: ACT - Australia
Distribution: Opensuse x86_64 (Latest)
Posts: 132

Rep: Reputation: 26
How do I insert a line/value after a particular line, in file with sed


Hello
I need to run a command in a shell script to insert a line in a file, after it finds certain line.

To add the line 'user = mysql'
after the line [mysqld]
in file /etc/my.cnf

How can that be done ?
I am thinking of using sed to accomplish it.

An example is appreciated.
Cheers Glenn
 
Old 01-13-2010, 02:47 AM   #2
Ni2
LQ Newbie
 
Registered: Nov 2004
Posts: 6

Rep: Reputation: 0
try

awk '{if (/\[mysqld\]/) {print $0 "\nuser = mysql"} else {print $0} }' /etc/my.cnf > my.cnf.modified
 
Old 01-20-2010, 01:29 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
or with sed it would be:

sed -i -e '/[mysqld]/auser = mysql' /etc/my.cnf
 
Old 01-21-2010, 09:14 PM   #4
BenCollver
Rogue Class
 
Registered: Sep 2006
Location: OR, USA
Distribution: Slackware64-15.0
Posts: 374
Blog Entries: 2

Rep: Reputation: 172Reputation: 172
sed -e 's/\[mysqld\]/&^Muser = mysql/'

Where ^M is Control-V Control-M in vim on 'nix.
 
  


Reply



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
sed insert # at the beginning of a line ilo Linux - Newbie 17 12-19-2012 08:21 AM
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 09:23 AM
sed insert line action_owl Linux - Newbie 3 08-24-2009 12:06 AM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 07:14 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:09 AM.

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
Open Source Consulting | Domain Registration