sed awk append after a line

I have a text file "file.txt"
#cat "file.txt"
line 1
line 2
line 3
line 5
line 6
#

I wanted to use sed/awk to append "line 4" after "line 3"

so the file should look like this
line 1
line 2
line 3
line 4
line 5
line 6
LVL 7
martin_2110Asked:
Who is Participating?
 
ghostdog74Commented:

awk '/^#<\/IfModule>/{
    getline
    print
    print "#</IfModule>"
    print "#End of proxy directives."
    next
}1' file

Open in new window

0
 
ozoCommented:
sed '3a\
line 4
'
0
 
Kerem ERSOYPresidentCommented:
Hi,

cat file.txt | sed -e 's/line 3/line 3\nline 4/'

Will do.

Cheers,
K.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Kerem ERSOYPresidentCommented:
of course you need to redirect the output to a temporary file and remove with the original such as this:

cat file.txt | sed -e 's/line 3/line 3\nline 4/' > /tmp/filenew ; mv /tmp/filenew file.txt

Cheers,
K.
0
 
ozoCommented:
# cat file.txt
line 1
line 2
line 3
line 5
line 6
# sed -i.bak '3a\
line 4
' file.txt
# cat file.txt
line 1
line 2
line 3
line 4
line 5
line 6
0
 
martin_2110Author Commented:
Any one got something a little less quick and dirty. Perhaps something with awk? My lines are not as simple as my example Think of lines with lots of special chars.
0
 
Kerem ERSOYPresidentCommented:
Yeah but in any case you'll escape any special character with \

be it sed or awk you need to escape them porperly for your regexp processor won't misinterpret them ..
0
 
ozoCommented:
Do you need to append special characters?
Are special characters needed to identify the line to append after?
0
 
Kerem ERSOYPresidentCommented:
yeah can you post an example ??

As far as I understand it is not in a fixed position. So what you need is just find and replace. But you say the regexp for match are rather complex.


0
 
martin_2110Author Commented:
Something like this. I want to insert the bold lines.

# To enable a cache of proxied content, uncomment the following lines.
# See http://httpd.apache.org/docs/2.2/mod/mod_cache.html for more details.
#
#<IfModule mod_disk_cache.c>
#   CacheEnable disk /
#   CacheRoot "/var/cache/mod_proxy"
#</IfModule>
#
#</IfModule># End of proxy directives.
### Section 3: Virtual Hosts
#
# VirtualHost: If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.


0
 
Kerem ERSOYPresidentCommented:
sed -i "s/### Section 3: Virtual Hosts/#\</IfModule\>\n# End of proxy directives.\n### Section 3: Virtual Hosts/"  httpd.conf
0
 
ozoCommented:
sed '/#<\/IfModule>$/a\
# End of proxy directives.
'  file.txt
0
 
Kerem ERSOYPresidentCommented:
oops sorry I've forgotten to escape some chars. The true working version is here:

sed -i "s/### Section 3\: Virtual Hosts/#\<\/IfModule\>\n# End of proxy directives.\n\n### Section 3: Virtual Hosts/"  httpd.conf
0
 
Kerem ERSOYPresidentCommented:
or better:
sed -i "s/### Section 3\: Virtual Hosts/#\<\/IfModule\>\n# End of proxy directives.\n\n&/"  httpd.conf

& will expand to the matching string
0
 
martin_2110Author Commented:
Thanks both. The awk solution was what i was looking for.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.