Nau
Nau

Reputation: 479

Delete lines in a text file that contain a specific string with /

I have a text file with dates in this format: 27/8/2019 I would like to remove all lines except for those that contain 27/8/2019

If I use sed, that would be:

sed -i '/pattern/!d' file.txt

The problem is that when I have a pattern with '/', I have the next error:

sed: -e expression #1, char 5: unknown command: `8'

Upvotes: 1

Views: 1957

Answers (3)

Penny Liu
Penny Liu

Reputation: 17488

It's easier to use grep in this case.

grep '27/8/2019' file.txt

Upvotes: 0

Jotne
Jotne

Reputation: 41460

Using awk

awk '/27\/8\/2019/' file.txt

If date is just a sample, use this to keep all date line:

awk '/[0-9]+\/[0-9]+\/[0-9]+/' file.txt

To update file inline with awk

awk '-your code-' file.txt > tmp && mv tmp file.txt

Upvotes: 0

Beta
Beta

Reputation: 99172

Escape the slashes:

sed -i '/27\/8\/2019/!d' file.txt

Upvotes: 3

Related Questions