Solved

sed awk append after a line

Posted on 2009-06-30
15
1,129 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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 …

920 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

16 Experts available now in Live!

Get 1:1 Help Now