Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sed awk append after a line

Posted on 2009-06-30
15
Medium Priority
?
1,144 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
0
Comment
Question by:martin_2110
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24750486
sed '3a\
line 4
'
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24750487
Hi,

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

Will do.

Cheers,
K.
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24750512
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
Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

 
LVL 84

Expert Comment

by:ozo
ID: 24750539
# 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
 
LVL 7

Author Comment

by:martin_2110
ID: 24750557
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24750580
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
 
LVL 84

Expert Comment

by:ozo
ID: 24750621
Do you need to append special characters?
Are special characters needed to identify the line to append after?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24750640
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
 
LVL 7

Author Comment

by:martin_2110
ID: 24750649
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24750676
sed -i "s/### Section 3: Virtual Hosts/#\</IfModule\>\n# End of proxy directives.\n### Section 3: Virtual Hosts/"  httpd.conf
0
 
LVL 84

Expert Comment

by:ozo
ID: 24750701
sed '/#<\/IfModule>$/a\
# End of proxy directives.
'  file.txt
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24750708
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
 
LVL 30

Assisted Solution

by:Kerem ERSOY
Kerem ERSOY earned 600 total points
ID: 24750741
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
 
LVL 9

Accepted Solution

by:
ghostdog74 earned 1400 total points
ID: 24751986

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

Open in new window

0
 
LVL 7

Author Closing Comment

by:martin_2110
ID: 31598574
Thanks both. The awk solution was what i was looking for.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Make the most of your online learning experience.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Introduction to Processes
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question