5

I have a file which contain following lines.

**;**;**;
*(Eng_Sen:This is an apple)*;*(WordID:1 2 3 4)*;*(message:)*;
**;**;**

I want to delete all the lines which contain following patterns using Sed command.

**;**;**;
**;**;**

Please help me. Thanks.

1
  • Why sed in particular? Considering the particular characters involved, it would be hard to read in those patterns from a file or something.
    – muru
    Feb 20, 2016 at 8:44

4 Answers 4

2
sed '/\*\*;\*\*;\*\*;\?/d' file
  • \*\*;\*\*;\*\*;\?: matches a **;**;** string optionally followed by a ; character;
  • d: deletes the line.

To edit the file in place:

sed -i '/\*\*;\*\*;\*\*;\?/d' file
% cat file                                          :(
**;**;**;
*(Eng_Sen:This is an apple)*;*(WordID:1 2 3 4)*;*(message:)*;
**;**;**
% sed '/\*\*;\*\*;\*\*;\?/d' file
*(Eng_Sen:This is an apple)*;*(WordID:1 2 3 4)*;*(message:)*;
% 
0
1

More generically, to remove any line that only contains semicolons and asterisks:

sed '/^[*;]\+$/d'

Although you don't need to restrict yourself to sed:

grep -Ev '^[*;]+$'
0

kos's answer already does the job. My answer only improves the regex somewhat.

By using the grouping pattern \( \) and the escaped .\+ we can tell it to match the whole group **; and whatever else is followed. In this example I am using

$  sed -e "s|\(\*\*;\).\+|TEST|g"  stars.txt                            
TEST
*(Eng_Sen:This is an apple)TEST
TEST
TEST
*(Eng_Sen:This is an apple)TEST
TEST

Note that GNU sed version can take any character as separator in the substitution command, not just /. Here I was using vertical bar.

In the example above ,I've taken the liberty to increase the OP's example from 3 to 6 lines.

$ cat stars.txt                                                
**;**;**;
*(Eng_Sen:This is an apple)*;*(WordID:1 2 3 4)*;*(message:)*;
**;**;**
**;**;**;
*(Eng_Sen:This is an apple)*;*(WordID:1 2 3 4)*;*(message:)*;
**;**;**
0

You can use Vim in Ex mode:

ex -sc 'g/\*\*/d' -cx file
  1. g global search

  2. d delete

  3. x save and close

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .