noway
noway

Reputation: 2585

Finding a string in a file and deleting all the text next to it in bash

I have the following text in a file, and I would like to find the word fox and would like to delete the text from the word fox till the end of the text. (including fox)

The quick 
brown fox 
jumps over 
the lazy dog

I know the /d switch but it only deletes the line.

sed -i '/fox/d' file

The requested outcome will be:

The quick 
brown

Upvotes: 0

Views: 33

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

Try this sed command,

sed '/fox/q' file | sed 's/fox//'

Example:

$ cat c
The quick 
brown  fox blah fox 
jumps over 
the lazy dog

$ sed '/fox/q' c | sed 's/fox.*//'
The quick 
brown  

Upvotes: 2

anubhava
anubhava

Reputation: 785156

You can use this sed:

sed '/fox/{s/fox//;q;}' file
The quick
brown

Upvotes: 2

Related Questions