1

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.

2
  • 2
    sed, grep, awk all do that. What did you try? Commented Dec 23, 2014 at 19:31
  • grep -v 21-12-2014 filename.txt? awk '! /21-12-2014/' filename.txt? sed -e '/21-12-2014/d' < filename.txt? Python or Perl are equally capable of doing this simply, and there's probably a number of other solutions... Commented Dec 23, 2014 at 21:33

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

Besides, that deletes the string, not the line. you need the d command.
Sputnick. How would I include an 'and' expressions. So I want to delete all lines with 21-12-2014 AND 60%?
sed -i.bak '/21-12-2014.*60%/d' * maybe the time to learn regex and understand what involves ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.