Skip to main content
Post Made Community Wiki by steeldriver
Added extra `q` so as to exit (and not append new end tag) when search string not found.
Source Link
Ned64
  • 9.3k
  • 9
  • 58
  • 94

It sounds like the sequence of commands you're looking for is

/END DATA/,$d
q
.a
NEW END
.
wq

or as a one-liner

printf '%s\n' '/END DATA/,$d' 'q' '.a' 'NEW END' '.' 'wq'

(You can replace wq with ,p for testing.)

Ex. given

$ cat file
Data 1
Data 2
something_unimportant_here END DATA
Rubbish 1
Rubbish 2

then

$ printf '%s\n' '/END DATA/,$d' 'q' '.a' 'NEW END' '.' 'wq' | ed -s file

gives

$ cat file
Data 1
Data 2
NEW END

It sounds like the sequence of commands you're looking for is

/END DATA/,$d
.a
NEW END
.
wq

or as a one-liner

printf '%s\n' '/END DATA/,$d' '.a' 'NEW END' '.' 'wq'

(You can replace wq with ,p for testing.)

Ex. given

$ cat file
Data 1
Data 2
something_unimportant_here END DATA
Rubbish 1
Rubbish 2

then

$ printf '%s\n' '/END DATA/,$d' '.a' 'NEW END' '.' 'wq' | ed -s file

gives

$ cat file
Data 1
Data 2
NEW END

It sounds like the sequence of commands you're looking for is

/END DATA/,$d
q
.a
NEW END
.
wq

or as a one-liner

printf '%s\n' '/END DATA/,$d' 'q' '.a' 'NEW END' '.' 'wq'

(You can replace wq with ,p for testing.)

Ex. given

$ cat file
Data 1
Data 2
something_unimportant_here END DATA
Rubbish 1
Rubbish 2

then

$ printf '%s\n' '/END DATA/,$d' 'q' '.a' 'NEW END' '.' 'wq' | ed -s file

gives

$ cat file
Data 1
Data 2
NEW END
Source Link
steeldriver
  • 83.9k
  • 12
  • 124
  • 175

It sounds like the sequence of commands you're looking for is

/END DATA/,$d
.a
NEW END
.
wq

or as a one-liner

printf '%s\n' '/END DATA/,$d' '.a' 'NEW END' '.' 'wq'

(You can replace wq with ,p for testing.)

Ex. given

$ cat file
Data 1
Data 2
something_unimportant_here END DATA
Rubbish 1
Rubbish 2

then

$ printf '%s\n' '/END DATA/,$d' '.a' 'NEW END' '.' 'wq' | ed -s file

gives

$ cat file
Data 1
Data 2
NEW END