Valiceemo
Valiceemo

Reputation: 68

shell command delete line in text file with specific text in line

In looking for a command to delete a line (or lines) from a text file that contain a certain string. For example I have a text file as follows

Sat 21-12-2014,10.21,78%
Sat 21-12-2014,11.21,60%
Sun 22-12-2014,09.09,21%

I want to delete all lines that have "21-12-2014" in them. I'm not able to find a solution that works.

Upvotes: 1

Views: 1889

Answers (2)

Arnab Nandy
Arnab Nandy

Reputation: 6712

According to @twalberg there is more three alternate solution for this question, which I'm explaining is as follows for future reader of this question for more versatile solutions:

With grep command

grep -v 21-12-2014 filename.txt

explanations:

-v is used to find non-matching lines

With awk command

awk '! /21-12-2014/' filename.txt

explanations:

! is denoting it will print all other lines that contain match of the string. It is not operator signify ignorance.

With sed command

sed -e '/21-12-2014/d' < filename.txt

explanations:

-e is signify scripted regex to be executed

d is denoting delete any match

< is redirecting the input file content to command

Upvotes: 1

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185790

Try doing this :

sed -i.bak '/21-12-2014/d' *

A bit of explanations :

  • sed : the main command line, put the mouse pointer on
  • -i.bak : replace the file in place and make a backup in a .bak file
  • // is the
  • d means: delete

Upvotes: 0

Related Questions