replacing with sed

replacing with sed

Post by Derek Douvil » Thu, 18 Jul 1996 04:00:00



Hello,  I've got 2 sed questions:

(1)  I want to take several lines of text that all contain a common
pattern and replace them with lines of text from another file.  The lines
are all in one contingious block (i.e.  they are all one after the other).
So in regexp talk,  I want to match everything from the first occurance of
_pattern_ through to the last occurance of _pattern_,  delete it,  and then
at that spot in the file,  read in another file.  How do I do this with sed?

(2)  I want to match everything in a file up to the first blank space.  With
an ed(1) command, I would simply do

        :g/.*//^$/

 but this doesn't seem to work in sed.  Also,  for both of these commands,  I
want to issue the edit on the command line (using -e),  not from a file.
Any ideas?

Please reply to news,  my e-mail may bounce.
--

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Derek Douville                                   BELL SYGMA - TELECOM SOLUTIONS    /
UNIX Technical Support (Internal)                         System Administration   /
Phone:  (613) 781-8242                              E-Mail: bouncy bouncy bounce /
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

 
 
 

replacing with sed

Post by Wis Macoms » Fri, 19 Jul 1996 04:00:00



>Hello,  I've got 2 sed questions:

>(1)  I want to take several lines of text that all contain a common
>pattern and replace them with lines of text from another file.  The lines
>are all in one contingious block (i.e.  they are all one after the other).
>So in regexp talk,  I want to match everything from the first occurance of
>_pattern_ through to the last occurance of _pattern_,  delete it,  and then
>at that spot in the file,  read in another file.  How do I do this with sed?

Try:

sed \
-e '/_pattern_/{r _IncludedTextFile_
: loop
N
/_pattern_/s/^.*$//
t loop
s/\n//

Quote:}' < _InputFile_

>(2)  I want to match everything in a file up to the first blank space.  With
>an ed(1) command, I would simply do

>    :g/.*//^$/

> but this doesn't seem to work in sed.  Also,  for both of these commands,  I
>want to issue the edit on the command line (using -e),  not from a file.
>Any ideas?

I interpret this as: "from the first line of the file until the first blank
line encountered."  Try this:

        sed '1,/^$/ d' _file_

The '<space>' before the 'd' is often significant.  "-e" isn't needed for
a single command.  Are you stripping mail headers? :)

Quote:

>Please reply to news,  my e-mail may bounce.
>--

>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>Derek Douville                            BELL SYGMA - TELECOM SOLUTIONS    /
>UNIX Technical Support (Internal)         System Administration   /
>Phone:  (613) 781-8242                    E-Mail: bouncy bouncy bounce /
>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


 
 
 

replacing with sed

Post by Derek Douvil » Tue, 23 Jul 1996 04:00:00



: >Hello,  I've got 2 sed questions:
: >
: >(1)  I want to take several lines of text that all contain a common
: >pattern and replace them with lines of text from another file.  The lines
: >are all in one contingious block (i.e.  they are all one after the other).
: >So in regexp talk,  I want to match everything from the first occurance of
: >_pattern_ through to the last occurance of _pattern_,  delete it,  and then
: >at that spot in the file,  read in another file.  How do I do this with sed?
:
: Try:
:
: sed \
: -e '/_pattern_/{r _IncludedTextFile_
: : loop
: N
: /_pattern_/s/^.*$//
: t loop
: s/\n//
: }' < _InputFile_

        Is there a way to do the above on a single command line,  or
an easy way to embed it into a shell command file?

: I interpret this as: "from the first line of the file until the first blank
: line encountered."  Try this:
:
:         sed '1,/^$/ d' _file_
:
: The '<space>' before the 'd' is often significant.  "-e" isn't needed for
: a single command.  Are you stripping mail headers? :)

        Actually,  I'm going through our master automounter map and
having a script insert a new line for a newly created account.  The
section affected is everything containing /home at the beginning of
a line.  This section must be sorted (alphabetically),  and then
re-inserted back into the file,  in the right place.  It just so happens
that there's stuff above and below this block.  (I suppose I could move
it to the end of the auto.common,  but then I wouldn't need to learn
more about sed -- which is pretty much the point).  ;-)

I'm working my way through one of O'Reilly's Perl books,  but I'm not
comfortable enough with Perl to do anything real yet.

: >Please reply to news,  my e-mail may bounce.

--

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Derek Douville                                  BELL SYGMA - TELECOM SOLUTIONS
UNIX Technical Support (Internal)                        System Administration
Phone:  (613) 781-8242                            E-Mail: bouncy bouncy bounce
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

 
 
 

1. replace gnu sed command ~ with sed command

Hi,

I've written a little sed script which is working in my environment. But
  I tried it in another environment and there it didn't work because the
~ command was unknown there.

Here the script:
# beginning at line 3, remove every 8th line
# beginning at line 4, remove every 8th line
# beginning at line 5, remove every 8th line
# beginning at line 6, remove every 8th line
sed -e '3~8d
         4~8d
         5~8d
         6~8d' file1.txt > file2.txt

How could I replace this gnu sed command with a sed command which is
valid in all environments?

I tried different things, but they didn't work.

For example I tried to replace this command as follows:

sed -e '3,${d;n;n;n;n;n;n;n}
         4,${d;n;n;n;n;n;n;n}
         5,${d;n;n;n;n;n;n;n}
         6,${d;n;n;n;n;n;n;n}' file1.txt > file2.txt

What am I doing wrong?

I'm new to sed, but I really like it and want to learn more about it and
I'm very interested in your answers.

Thanks for your help

Dirk

2. Bash: How to create file whose name include "/"?

3. search and replace in sed

4. Wrong device in /etc/fstab

5. Replace with SED, slashes in path

6. Good Linux Setup

7. $Q$ Case insensitive replace in sed

8. Is Solaris CDE anything like HP VUE?

9. Search and Replace using sed and shell variable

10. Help replacing with SED...

11. Replace in sed

12. Case insensitive replace in sed

13. replacing with sed