LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Insert line into a file with a command (https://www.linuxquestions.org/questions/linux-newbie-8/insert-line-into-a-file-with-a-command-779774/)

guif 01-04-2010 01:52 PM

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"

????

thanks!

rweaver 01-04-2010 01:58 PM

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.

guif 01-04-2010 02:43 PM

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?


All times are GMT -5. The time now is 07:26 AM.