Replace string starting from the 2nd occurrence using sed?

Replace string starting from the 2nd occurrence using sed?

Post by Suha » Wed, 10 Sep 2003 00:14:14



Hello Everyone,

I have this text file that looks like this:

Sales Order Loader
[text]
[text]

Sales Order Loader
[text]
[text]
[text]
.
.
and so on....

I would like to replace the string "Sales Order Loader" to "^LSales
Order Loader" (adding a page-break) starting from the 2nd occurrence.
How can I do this?

Thanks for any suggestions.
Suhas

 
 
 

Replace string starting from the 2nd occurrence using sed?

Post by Moshe Jacobso » Wed, 10 Sep 2003 00:19:07


Suhas had nothing better to do than to say:

Quote:> Sales Order Loader
> [text]
> [text]
> Sales Order Loader
> [text]
> [text]
> [text]
> .
> .
> I would like to replace the string "Sales Order Loader" to "^LSales
> Order Loader" (adding a page-break) starting from the 2nd occurrence.

If the first occurence is on the first line of the file, you can tell
sed to replace on every line but the first:

sed -e '!1 s/^Sales Order Loader/^L&/'

You'd insert the ^L as an actual control code, not a caret (^) with an
L following it.

Moshe

--
*** SPAM BLOCK: Remove bra before replying! ***
http://runslinux.net :: moshe at runslinux dot net :: AIM: Jehsom

 
 
 

Replace string starting from the 2nd occurrence using sed?

Post by Ed Morto » Wed, 10 Sep 2003 00:43:17



Quote:> Hello Everyone,

> I have this text file that looks like this:

> Sales Order Loader
> [text]
> [text]

> Sales Order Loader
> [text]
> [text]
> [text]
> .
> .
> and so on....

> I would like to replace the string "Sales Order Loader" to "^LSales
> Order Loader" (adding a page-break) starting from the 2nd occurrence.
> How can I do this?

> Thanks for any suggestions.
> Suhas

awk '/^Sales Order Loader$/ { if (f == 1) printf("^L"); f = 1 }
        { print }'

The "^L" is, of course, intended to be "<Control-L>".

Regards,

        Ed.

 
 
 

Replace string starting from the 2nd occurrence using sed?

Post by John W. Krah » Wed, 10 Sep 2003 03:38:25



> I have this text file that looks like this:

> Sales Order Loader
> [text]
> [text]

> Sales Order Loader
> [text]
> [text]
> [text]
> .
> .
> and so on....

> I would like to replace the string "Sales Order Loader" to "^LSales
> Order Loader" (adding a page-break) starting from the 2nd occurrence.
> How can I do this?

perl -i~ -pe'?^Sales Order Loader?&&next;s/^(?=Sales Order Loader)/\f/' yourfile.txt

John
--
use Perl;
program
fulfillment

 
 
 

Replace string starting from the 2nd occurrence using sed?

Post by John W. Krah » Wed, 10 Sep 2003 04:59:54




> > I have this text file that looks like this:

> > Sales Order Loader
> > [text]
> > [text]

> > Sales Order Loader
> > [text]
> > [text]
> > [text]
> > .
> > .
> > and so on....

> > I would like to replace the string "Sales Order Loader" to "^LSales
> > Order Loader" (adding a page-break) starting from the 2nd occurrence.
> > How can I do this?

> perl -i~ -pe'?^Sales Order Loader?&&next;s/^(?=Sales Order Loader)/\f/' yourfile.txt

Or, a bit shorter:

perl -i~ -pe'?^Sales Order Loader?||s/^(?=Sales Order Loader)/\f/'
yourfile.txt

John
--
use Perl;
program
fulfillment

 
 
 

Replace string starting from the 2nd occurrence using sed?

Post by Damian Ibbotso » Wed, 10 Sep 2003 19:29:11


Using sed (and assuming that your pattern might not be on the
first line)...

# if input line matches 'Sales Order Loader'

/Sales Order Loader/ {

# swap pattern space and hold space

x

# if 'new' pattern space is not blank (i.e. not the first)

/./ {

# swap back to original pattern space

x

# perform substitution

s/Sales/^LSales/

# swap pattern and hold space again (to sync)

x

Quote:}

# swap back to original pattern space

x

# replace hold space with pattern space

h

Quote:}

I don't claim to be particularly proficient in sed, so this might be a
bit long-winded (but it works).

--
Posted via http://dbforums.com

 
 
 

Replace string starting from the 2nd occurrence using sed?

Post by laura fairhe » Thu, 11 Sep 2003 04:15:30



Quote:>Hello Everyone,

>I have this text file that looks like this:

>Sales Order Loader
>[text]
>[text]

>Sales Order Loader
>[text]
>[text]
>[text]
>.
>.
>and so on....

>I would like to replace the string "Sales Order Loader" to "^LSales
>Order Loader" (adding a page-break) starting from the 2nd occurrence.
>How can I do this?

When you're satified it works okay you could add the -i option
so perl will modify the file,

perl -pe 's/Sales Order Loader/($f++?"\x0C":"").$&/e' filename

Quote:

>Thanks for any suggestions.
>Suhas

byefornow
l

--

 
 
 

Replace string starting from the 2nd occurrence using sed?

Post by rakesh shar » Thu, 11 Sep 2003 06:33:35



> Hello Everyone,

> I have this text file that looks like this:

> Sales Order Loader
> [text]
> [text]

> Sales Order Loader
> [text]
> [text]
> [text]
> .
> .
> and so on....

> I would like to replace the string "Sales Order Loader" to "^LSales
> Order Loader" (adding a page-break) starting from the 2nd occurrence.
> How can I do this?

> Thanks for any suggestions.
> Suhas

sed -e '
   # initialize hold space to null
   1{
      x;s/.*//;x
   }
   /Sales Order Loader/{
      # enquire about hold-space status
      x
      /^$/{
          # first-time occurance, set hold to nonnull,e.g.,X
          s//X/
          x;b
      }
      # second/more occurance
      x;s/Sales Order Loader/^L&/
   }
' textfile
 
 
 

1. Korn Shell: Using sed to replace string with multiline string?

I am trying to use the sed command to replace a single line within a
file with a multi-line string. The command format is:

sed s"/|-insert_data_here-|/$data_string/"

The data_string contain the contents of a file (10 lines).  Sed
returnes the following error message when it encounters the first
newline embedded within data_string.

sed: command garbled:

How can I get sed to treat the data_string as just a stream of
characters without getting hung up on the embedded newline characters?

2. Disk crash

3. 2nd last word occurrence using sed.

4. HP Vectra QS/20 Bios

5. Search and Replace all occurrences of a string in a file

6. Solaris 2.6 Printing problems.

7. Can I replace a section of string/file using sed?

8. Uptime monitoring utility

9. Counting char-string occurrences with sed ?

10. Replace URL string using sed

11. Question: Using SED to replace strings in multiple files

12. sed: find 2nd last occurrence

13. using sed to replace s string in a file : helpme