0

I need to remove a line which starts like this:

[success] Total ....

I tried the following sed command but it didn't work

sed '/\[/d' filename > newFile

But when I greped the new file the line is still there! What is the correct command to get rid of it

C.S
  • 73
  • 5
  • 2
    Cannot reproduce. This works as expected for me. Are you using gnu sed ? Or an other version on macOS/Windows ? Note that with gnu sed you can use the '-i' option to overwrite the file in place (does not handle backups): `sed -i '/\[/d' filename` – Zeitounator May 05 '19 at 22:54
  • 1
    No I'm using Linux GNU sed. I have no idea why it is not working. The file is 10GB in size though. Maybe it can't handle it – C.S May 05 '19 at 23:10
  • 1
    To my knowledge, you only get problems with sed when your lines in your file are extremely long. For the rest, it streams the content so filesize should not be a problem. Anyway, you should get an error if it cannot handle it. – Zeitounator May 05 '19 at 23:23
  • 2
    wrt `it didn't work` - never just say **that**. Tell us in what way it didn't work (wrong output, no output, error message, core dump, etc.) and post any error messages. [edit] your question to show a [mcve] that includes concise, testable sample input (with the target line(s) **in context**) and expected output and the actual output you're getting – Ed Morton May 06 '19 at 00:15

3 Answers3

2

If you are expecting to delete a line that starts with a pattern, then you should use the anchor symbol (^) in the beginning of the pattern :

sed -E '/^\[/d' filename > newFile

To accommodate blank spaces in the beginning of the pattern which is common as a result of indentation, you should do

sed -E '/^[[:blank:]]+\[/d' filename > newFile

GNU sed has an inplace-edit option realized thru -i option, so above could be replaced by

sed -Ei '/^[[:blank:]]+\[/d' filename

It seems that sed has no limits concerning file size but it is typically slow for a large file.

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • 3
    `+` will not work without `-E`, which is a GNU extension. even if you escape it, it's not a part of POSIX BREs – oguz ismail May 06 '19 at 01:24
  • 1
    @oguzismail I have only worked with GNU sed that I often forget about other versions. You have a point. Thank you. – sjsam May 06 '19 at 08:11
0

To delete a line starting with [ we can use this sed pattern

$ sed -E '/^\s*\[/d' filename

Say for file data3.txt having this content:

$ cat data3.txt
dont remove this line
[success] faile ... remove this line
[hola to be removed
[hola] remove this
 [hola]  remove this
[] remove thisline
this [success] Total dont remove this

You can run this command which takes care of blank/tab/space etc. at the beginning followed by [ and other text data:

$ sed '/^\s*\[/d' data3.txt
dont remove this line
this [success] Total dont remove this

Here

'/^\s*\[/d' : takes care of [ preceded by zero or more occurrences of space/tab etc.
alphaGeek
  • 442
  • 1
  • 7
  • 19
  • As I understand, `-E/-r` has no effect on `*`, `[` or `]` but only on `?`,`+`,`{`,`}` and `|`. In GNU sed the latter list can be made _special_ by preceding each by `\\` without the need for former option. – potong May 06 '19 at 10:41
  • @potong you are right, -E has no effect on *. edited my answer. thanks for pointing out. – alphaGeek May 07 '19 at 06:00
0

Use grep:

grep -v '^[[]' file

To match the [, I normally put it into a character class:

[]  # empty character class
[[] # character class with [ as the only item

Btw: If there are optional spaces allowed at the beginning of the line:

grep -v '^[[:blank:]]*[[]' file
hek2mgl
  • 152,036
  • 28
  • 249
  • 266