Results 1 to 6 of 6
Hi Everybody,
sed '/<pattern>/r <file1>' <file2>
This adds the contents of file1 in file2 after pattern,
But I want to insert some data from a file before a pattern . ...
- 06-29-2007 #1Linux Newbie
- Join Date
- Jul 2004
- Posts
- 143
reg sed
Hi Everybody,
sed '/<pattern>/r <file1>' <file2>
This adds the contents of file1 in file2 after pattern,
But I want to insert some data from a file before a pattern. How to do that by using sed.
Please suggest me...
Thanks Inadvance
Mummaneni.
- 06-30-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Can you include a sample of the files and what you wish to accomplish?
Regards
- 06-30-2007 #3Linux Newbie
- Join Date
- Jul 2004
- Posts
- 143
Hi,
lets assume the contents of the files:
file1 contains
one
two
three
four
and file2 contains
seven
eight
nine
by using sed, we can insert the data of a file after a pattern
sed '/three/r file1' file2
now the O/P will be:
one
two
three
seven
eight
nine
four
But I want to add contents of file2 before three of file1
for ex:
one
two
seven
eight
nine
three
four
This is my requirement.. please help me out
Thanks & Regards,
Mummaneni.
- 06-30-2007 #4Linux User
- Join Date
- Jun 2007
- Posts
- 318
I think I see what he wants.
Say file1 contains:
aaa
bbb
ccc
ddd
eee
file2 contains:
111
222
333
444
555
If /<pattern>/ is /333/ he wants the result to be:
111
222
aaa
bbb
ccc
ddd
eee
333
444
555
I came up with not one sed command but this:
sed '/333/,$d' file2; cat file1; sed -n '/333/,$p' file2
I know it's a kludge but it works.
- 06-30-2007 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
If the file is not too long, you can script ed. It can do arithmetic on line addresses:
producing:Code:#!/bin/sh # @(#) s2 Demonstrate ed insert. set -o nounset echo " sh version: $BASH_VERSION" cat >data1 <<EOF one two three four five EOF cat >data2 <<EOF 3.1 3.2 EOF ed data1 <<EOF /four/-1 . r data2 w q EOF cat -n data1 # See man ed for details. exit 0
cheers, drlCode:% ./s2 sh version: 2.05b.0(1)-release 24 three 8 32 1 one 2 two 3 three 4 3.1 5 3.2 6 four 7 fiveWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 06-30-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631


Reply With Quote
