Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-04-2010, 02:52 PM   #1
LQ Newbie
Registered: Dec 2008
Posts: 24

Rep: Reputation: 15
Insert line into a file with a command

I would like to insert a line into the /etc/sysconfig/iptables with a command like sed or with any script.

How can I insert this line:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

but under the line "-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT"


Old 01-04-2010, 02:58 PM   #2
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
I'd use a text editor unless this is something you're going to be doing so often that it needs a script to automate it for some reason.

That being said, you could read the file in via perl or awk and parse it, when you find the value you need insert the next line into the file and continue along. If it's the last line of the file you could just echo it. Or have a new file with just the commands from where you want that you could source from the script/configuration at the appropriate location.

Lots of ways to do it just not sure if it's really the best method available.
Old 01-04-2010, 03:43 PM   #3
LQ Newbie
Registered: Dec 2008
Posts: 24

Original Poster
Rep: Reputation: 15
I would like only to locate my value (for example "dport 80") and then insert under this line on line with my new rule.
How it's possible with awk?


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
insert a line or a file to /etc j-me Linux - Newbie 9 07-23-2009 02:47 PM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 08:14 PM
insert a line into a file: facing problem RudraB Programming 7 03-25-2008 08:43 PM
shell script: insert line in a file noir911 Programming 6 02-04-2008 11:42 PM
Script to insert string in first line of a file minil Programming 13 01-03-2006 12:56 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:04 PM.

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