Solved

sed awk append after a line

Posted on 2009-06-30
15
1,127 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 150 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 350 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now