0

I know how to match text using patterns but not how to manipulate them.

I have used to match and extract lines from a text file, but I want to remove those lines from the text. How can I achieve this without having to write a or shell script?

I have searched on Google and was recommended to use , but I am new to it and don't know how it works.

Can anyone point me in the right direction or help me achieve this goal?

0

2 Answers 2

2

The -v option to grep inverts the search, reporting only the lines that don't match the pattern.

Since you know how to use grep to find the lines to be deleted, using grep -v and the same pattern will give you all the lines to be kept. You can write that to a temporary file and then copy or move the temporary file over the original.

grep -v pattern original.file > tmp.file
mv tmp.file original.file

You can also use sed, as shown in shellfish's answer.

There are multiple possible refinements for the grep solution, but for most people most of the time, what is shown is more or less adequate (it would be a good idea to use a per process intermediate file name, preferably with a random name such as the mktemp command gives you). You can add code to remove the intermediate file on an interrupt; suppress interrupts while moving back; use copy and remove instead of move if the original file has multiple hard links or is a symlink; etc. The sed command more or less works around these issues for you, but it is not cognizant of multiple hard links or symlinks.

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

2 Comments

Of course, grep -v, such an elegant answer. Should have thought about that!
I have achieved it using grep -v. That works well.Thanks.
1

Create the pattern which matches the lines using . Then create a script as follows:

sed -i '/pattern/d' file

Explanation:

  • The -i option means overwrite the input file, thus removing the files matching pattern.
  • pattern is the pattern you created for grep, e.g. ^a*b\+.
  • d this sed command stands for delete, it will delete lines matching the pattern.
  • file this is the input file, it can consist of a relative or absolute path.

For more information see man sed.

2 Comments

Note that sed does not recognize + as a regex metacharacter by default, even with GNU sed. The sample pattern matches b+, aab+, but not aaabc. Add the -r option to enable extended regexes.
Of course, I was being sloppy. I just gave some random pattern off the top of my head. Thanks for pointing that out though!

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.