Reputation: 12829
I have a text file like
some
important
content
goes here
---from here--
some
unwanted content
I am trying to delete all lines after ---from here--
including ---from here--
. That is, the desired output is
some
important
content
goes here
I tried sed '1,/---from here--/!d' input.txt
but it's not removing the ---from here--
part. If I use sed '/---from here--.*/d' input.txt
, it's only removing ---from here--
text.
How can I remove lines after a pattern including that pattern?
EDIT
I can achieve it by doing the first operation and pipe its output to second, like sed '1,/---from here--/!d' input.txt | sed '/---from here--.*/d' > outputput.txt
.
Is there a single step solution?
Upvotes: 2
Views: 270
Reputation: 8711
You can try Perl
perl -ne ' $x++ if /---from here--/; print if !$x '
using your inputs..
$ cat johnykutty.txt
some
important
content
goes here
---from here--
some
unwanted content
$ perl -ne ' $x++ if /---from here--/; print if !$x ' johnykutty.txt
some
important
content
goes here
$
Upvotes: 0
Reputation: 5252
Another awk approach:
awk '/---from here--/{exit}1' file
If you have GNU awk 4.1.0+, you can add -i inplace
to change the file in-place.
Otherwise appened | tee file
to change the file in-place.
Upvotes: 2
Reputation: 133428
Could you please try following(in case you are ok with awk
).
awk '/--from here--/{found_from=1} !found_from{print}' Input_file
Upvotes: 1
Reputation: 12204
I'm not positive, but I believe this will work:
sed -n '/---from here--/q; p' file
The q
command tells sed
to quit processing input lines after matching a given line.
Upvotes: 1
Reputation: 15461
Another approach with sed:
sed '/---from here--/,$d' file
The d
(delete) command is applied to all lines from first line containing ---from here--
up to the end of file($
)
Upvotes: 3