LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 01:37 AM   #1
Glenn D.
Member
 
Registered: May 2009
Location: ACT - Australia
Distribution: Opensuse x86_64 (Latest)
Posts: 128

Rep: Reputation: 16
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, 03: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, 02:29 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

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

sed -i -e '/[mysqld]/auser = mysql' /etc/my.cnf
 
Old 01-21-2010, 10:14 PM   #4
BenCollver
Rogue Class
 
Registered: Sep 2006
Location: OR, USA
Distribution: Slackware 14.1
Posts: 201
Blog Entries: 3

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

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


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

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

All times are GMT -5. The time now is 10:03 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration