Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page reg sed
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 06-29-2007   #1 (permalink)
mummaneni
Linux Newbie
 
Join Date: Jul 2004
Posts: 144
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.
mummaneni is offline   Reply With Quote
Old 06-30-2007   #2 (permalink)
Franklin52
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 596
Can you include a sample of the files and what you wish to accomplish?

Regards
Franklin52 is offline   Reply With Quote
Old 06-30-2007   #3 (permalink)
mummaneni
Linux Newbie
 
Join Date: Jul 2004
Posts: 144
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.
mummaneni is offline   Reply With Quote
Old 06-30-2007   #4 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 209
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.
vsemaska is offline   Reply With Quote
Old 06-30-2007   #5 (permalink)
drl
Linux Enthusiast
 
drl's Avatar
 
Join Date: Apr 2006
Location: Saint Paul, MN, USA / CentOS, Solaris, SuSE, Xandros
Posts: 675
Hi.

If the file is not too long, you can script ed. It can do arithmetic on line addresses:
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
producing:
Code:
% ./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  five
cheers, drl
__________________
Welcome - 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 )
drl is offline   Reply With Quote
Old 06-30-2007   #6 (permalink)
Franklin52
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 596
Quote:
Originally Posted by vsemaska View Post
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.
This should work too:

Code:
sed '/333/{x;p;x;}' file2|sed -e '/^$/r file1' -e '/^$/d'
Regards
Franklin52 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:51 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0