We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

sed awk append after a line

Medium Priority
1,190 Views
Last Modified: 2012-05-07
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
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
sed '3a\
line 4
'
Kerem ERSOYPresident

Commented:
Hi,

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

Will do.

Cheers,
K.
Kerem ERSOYPresident

Commented:
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.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
# 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

Author

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.
Kerem ERSOYPresident

Commented:
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 ..
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Do you need to append special characters?
Are special characters needed to identify the line to append after?
Kerem ERSOYPresident

Commented:
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.


Author

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.


Kerem ERSOYPresident

Commented:
sed -i "s/### Section 3: Virtual Hosts/#\</IfModule\>\n# End of proxy directives.\n### Section 3: Virtual Hosts/"  httpd.conf
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
sed '/#<\/IfModule>$/a\
# End of proxy directives.
'  file.txt
Kerem ERSOYPresident

Commented:
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
Kerem ERSOYPresident
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks both. The awk solution was what i was looking for.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.