LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-03-2011, 08:02 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Rep: Reputation: 28
sed insert line with leading spaces


Hi,

I'm trying to insert a line using sed that has leading spaces before the text. Sed seems to be just dropping the spaces and only inserting the text. Any ideas what I'm missing?

Code:
NAM=rb134
sed -i.bak -e "\$i \
        host ${NAM} {" /etc/crap
Instead of inserting a line with 8 leading spaces inserts it with "host" at the beginning of the line. I tried
Code:
NAM=rb134
sed -i.bak -e "\$i \
^        host ${NAM} {" /etc/crap
but it put the "^" at the begging of the line.

Thanks!
 
Old 03-03-2011, 08:08 AM   #2
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Original Poster
Rep: Reputation: 28
Found a solution

I found a solution. Not sure if it is the best, but it works.

Code:
NAM=rb134
sed -i.bak -e "\$i \
\ \ \ \ \ \ \ \ host ${NAM} {" /etc/crap
 
Old 03-03-2011, 08:11 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Not actually the best, but it works indeed. To force sed to not ignore the leading spaces you can simply put a backslash at the beginning of the line:
Code:
NAM=rb134
sed -i.bak -e "\$i \
\        host ${NAM} {" /etc/crap
It is documented here: http://www.grymoire.com/Unix/Sed.html#uh-43.
 
1 members found this post helpful.
Old 03-03-2011, 08:14 AM   #4
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 697

Rep: Reputation: 93
Try this. It should just be another way to do the same thing.
If it works, I don't know why your version didn't work.

Code:
NAM=rb134
sed -i.bak '$i\
        host '"$NAM"' {' /etc/crap
 
1 members found this post helpful.
Old 03-03-2011, 08:45 AM   #5
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Original Poster
Rep: Reputation: 28
Thanks! I'll give it a try.
 
Old 03-04-2011, 04:40 AM   #6
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Original Poster
Rep: Reputation: 28
Both worked great. Thanks!!!
 
Old 03-04-2011, 04:51 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
You're welcome!
 
  


Reply

Tags
sed, shell


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 08:21 AM
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 09:23 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
sed insert line action_owl Linux - Newbie 3 08-24-2009 12:06 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 05:53 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration