Unlimited Usenet
day retention, 99% Completion, Unlimited Access, Free Trial!

Sed, insert a file current line

This is a discussion on Sed, insert a file current line within the Linux General forums, part of the Linux Forums category; Hi, It is quite easy to include/append a file after a criteria line in sed by '/criteria/ r file' ...


Go Back   Usenet Forums > Linux Forums > Linux General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-20-2003
* Tong *
 
Posts: n/a
Default Sed, insert a file current line

Hi,

It is quite easy to include/append a file after a criteria line in sed by

'/criteria/ r file'

command. But is it possible to insert a file before a criteria line in
sed? If not, is there another simple tool (not perl) that can do it?

Thanks


--
Tong (remove underscore(s) to reply)
*niX Power Tools Project: http://xpt.sourceforge.net/
- All free contribution & collection
Reply With Quote
  #2 (permalink)  
Old 11-20-2003
Robert Katz
 
Posts: n/a
Default Re: Sed, insert a file current line

* Tong * wrote:

> Hi,
>
> It is quite easy to include/append a file after a criteria line in sed by
>
> '/criteria/ r file'
>
> command. But is it possible to insert a file before a criteria line in
> sed? If not, is there another simple tool (not perl) that can do it?


awk '{if (/criteria/) system("cat file"); print}'

--
Regards,

---Robert

Reply With Quote
  #3 (permalink)  
Old 11-20-2003
rakesh sharma
 
Posts: n/a
Default Re: Sed, insert a file current line

* Tong * <sun_tong@users.sourceforge.net> wrote in message news:<292fd8a71b89d833cc53eb5191e5e726@news.terane ws.com>...
> Hi,
>
> It is quite easy to include/append a file after a criteria line in sed by
>
> '/criteria/ r file'
>
> command. But is it possible to insert a file before a criteria line in
> sed? If not, is there another simple tool (not perl) that can do it?
>
> Thanks


sed -e '
/criteria/{
x;p
r file
x
}
h
' yourfile

note: this is not thoroughly tested as consecutive /criteria/ lines
will misbehave.
Reply With Quote
  #4 (permalink)  
Old 11-20-2003
Tapani Tarvainen
 
Posts: n/a
Default Re: Sed, insert a file current line

* Tong * <sun_tong@users.sourceforge.net> writes:

> It is quite easy to include/append a file after a criteria line in sed by
>
> '/criteria/ r file'
>
> command. But is it possible to insert a file before a criteria line in
> sed?


The 'r' command actually outputs the file just before reading
a new line to the pattern buffer (or at EOF). That can be forced
in mid-script by 'n' or 'N', and while 'n' will also print the
buffer before 'r' does its thing, 'N' won't, so:

sed -e '/criteria/{r file' -e N -e '}'

or if criteria is a regexp (not line number), also

sed -e '/criteria/r file' -e //N

Those will fail if /criteria/ occurs twice in a row.
That is easily fixed if needed, however, just print only the
matched /criteria/ line and reprocess the newly read line:

sed -e '/criteria/{r file' -e 'N;P;D' -e '}'

Warning: If /criteria/ occurs on the very last line of the file,
all of the above will fail with some sed versions, namely those
that discard the pattern buffer if 'N' is executed at the last line.
Gnu sed is safe, with others you should test for that before
relying on these, unless you know /criteria/ cannot occur on the
last line. The only obvious workaround for such broken sed's is
appending a dummy line before the sed call and then deleting it:

(cat infile; echo) |
sed -e '$d' -e '/criteria/{r file' -e 'N;P;D' -e '}'

> If not, is there another simple tool (not perl) that can do it?


There are always alternative solutions in unix. E.g.,

awk '/criteria/{while (getline tmp <"file") print tmp} {print}'

or if criteria isn't a regexp,

while read line; do
case "$line" in *criteria*) cat file;; esac
printf "%s\n" "$line"
done <infile

--
Tapani Tarvainen
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:28 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0