LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-13-2011, 08:31 PM   #1
Mouse750
Member
 
Registered: Aug 2011
Distribution: Ubuntu
Posts: 50

Rep: Reputation: Disabled
Having a problem with sed.


This is the file I need help with:

Code:
# test.txt

config 'zone'
	option 'name' 'wan'
	option 'input' 'REJECT'
	option 'output' 'ACCEPT'
	option 'forward' 'REJECT'
	option 'masq' '1'
	option 'mtu_fix' '1'
I want to use sed to append a line after

Code:
	option 'name' 'wan'
So that it looks like:

Code:
# test.txt

config 'zone'
	option 'name' 'wan'
	option 'network' 'wan vpn'
	option 'input' 'REJECT'
	option 'output' 'ACCEPT'
	option 'forward' 'REJECT'
	option 'masq' '1'
	option 'mtu_fix' '1'
This is what I am trying:

Code:
sed -i "/option 'name' 'wan'/a \
	option 'network' 'wan vpn'" test.txt
And this is what I get:

Code:
# test.txt

config 'zone'
	option 'name' 'wan'
option 'network' 'wan vpn'
	option 'input' 'REJECT'
	option 'output' 'ACCEPT'
	option 'forward' 'REJECT'
	option 'masq' '1'
	option 'mtu_fix' '1'
I don't mean to be picky but can I get that 4th line to "tab" in? It just looks ugly.

-
 
Old 09-13-2011, 08:54 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Just put a backslash (\) before the tab where you want it in the command line.
 
1 members found this post helpful.
Old 09-13-2011, 09:37 PM   #3
Mouse750
Member
 
Registered: Aug 2011
Distribution: Ubuntu
Posts: 50

Original Poster
Rep: Reputation: Disabled
I can't even enter a "tab" into the command line. I tried pasting one in but, that did not work either. I can use 8 spaces to "look" like a tab like so:

Code:
sed -i "/option 'name' 'wan'/a \
\        option 'network' 'wan vpn'" test.txt
Thanks for your backslash tip, at least 8 spaces gives the optical illusion of a tab. I'll leave this thread open for a little while in case any one else has anything to add.

Last edited by Mouse750; 09-13-2011 at 09:43 PM.
 
Old 09-14-2011, 03:10 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
tab in CLI

Quote:
Originally Posted by Mouse750 View Post
I can't even enter a "tab" into the command line. I tried pasting one in but, that did not work either. I can use 8 spaces to "look" like a tab like so:

Code:
sed -i "/option 'name' 'wan'/a \
\        option 'network' 'wan vpn'" test.txt
Thanks for your backslash tip, at least 8 spaces gives the optical illusion of a tab. I'll leave this thread open for a little while in case any one else has anything to add.
Hi,

if you want to enter a tab in CLI you can
a) press CTRL+V and then CTRL+I
b) press CTRL+V and then TAB

Hope this helps.
 
Old 09-14-2011, 03:19 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
autoindent

This one will automatically use the indentation of the previous line:
Code:
sed -r "s/^(([[:blank:]]*)option 'name' 'wan'.*)$/\1\n\2APPEND/" file
The above works with GNU sed.

Last edited by crts; 09-14-2011 at 03:20 AM.
 
1 members found this post helpful.
Old 09-14-2011, 11:17 AM   #6
Mouse750
Member
 
Registered: Aug 2011
Distribution: Ubuntu
Posts: 50

Original Poster
Rep: Reputation: Disabled
Thank You!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
Problem with sed darkxer0x Programming 4 02-25-2009 07:55 PM
Another problem with sed maker10 Programming 6 07-21-2008 12:13 PM
sed problem fossilet Linux - Software 4 12-26-2004 05:44 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:19 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