Sed - Delete one or more lines from a file  

Sublime MultiEditUtils

图片 1

Build Status

A Sublime Text 2/3 Plugin which enhances editing of multiple selections. In case you aren't familar with Sublime's awesome multiple selection features, visit this page.


This is called as one execution cycle. Cycle continues till end of file/input is reached:

  1. Read a entire line from stdin/file.
  2. Removes any trailing newline.
  3. Places the line, in its pattern buffer.
  4. Modify the pattern buffer according to the supplied commands.
  5. Print the pattern buffer to stdout.





n : 匹配第几行
m,n : 从m到n行
m~n :从m行开始,每隔n行
'$' : 最后一行


/sometext/ : 匹配有sometext字符串的行

sed -n 'ADDRESS'p filename  // print, e.g: sed -n '4'p text.txt。 'ADDRESSp',即将p放在单引号中也是可以的,或者将单引号换成双引号"ADDRESSp"也是一样,下同。
sed -n '/PATTERN/p' filename    // e.g: sed -n '/aa/p' text.txt

sed -n 'ADDRESS'd filename  //delete,不会删除文件中的内容,只是在标准输出中删除该行。
sed -n '/PATTERN/d' filename

sed 'ADDRESS a
    some text'  filename    //append,在匹配行的下一行添加"some text",注意单引号
sed '/PATTERN/ a
    some text' filename

sed 'ADDRESS i
    some text' filename    // insert,在匹配行前插入一行
sed '/PATTERN/ i
    some text' filename

sed 'ADDRESS c
    some text' filename    // 替换匹配行
sed 'PATTERN c
    some text' filename

sed -n 'ADDRESS'= filename  //打印匹配的行号, 只接受一个地址,如果打印多行使用{}括起来,如下
}' filename


Extend the current selection with the last selection

Sometimes Sublime's standard features for creating multiple selections won't cut it. MultiEditUtils allows to select the desired parts individually and merge the selections with the add_last_selection command (default keybinding is ctrl/cmd+alt+u).

图片 2

sed 正则表达式查找和替换:

's' 命令应该是sed中最重要的命令,语法如下:



sed '1s/aa/bb/' text.txt        //用bb替换aa,只在aa第一次出现的地方进行替换
sed '2s/aa/bb/' text.txt    //用bb替换aa,只在aa第二次出现的地方进行替换
sed 's/aa/bb/g' text.txt    //用bb替换aa,g(global )是全局替换的意思。不加g默认也是全局替换。
sed -n 's/aa/bb/gpw result.txt' text.txt    //g全局替换,p打印,w将替换结果写到result.txt文件中

sed 's/...$//' text.txt        //正则表达式匹配:将最后位置的前三个字符替换为空,即删除每行的最后三个字符。


Split the selection

Sublime has a default command to split selections into lines, but sometimes you want to define your own splitting character(s). MultiEditUtils' split_selection command (default keybinding is ctrl/cmd+alt+,) will ask you for a separator and split the selection using your input. An empty separator will split the selection into its characters.

图片 3


sed -e: use multiple -e options to build up a script out of many parts。 -e option is optional for sed with single command. sed will execute the each set of command while processing input from the pattern buffer

-e script, --expression=script

    add the script to the commands to be executed

例子: sed -e 's/foo/bar/' -e '/FOO/d' 解释:先用bar替换foo,然后再删除有字符串FOO的行

sed工作原理 This is called as one execution cycle. Cycle continues till end of file/input is reached:

  1. Read a entire line from stdin/file. 2. Remove...

sed '{[/]<n>|<string>|<regex>[/]}d' <fileName>         

Preserve case while editing selection contents

When multi-selecting all occurences of an identifier it is cumbersome to change it to another one if the case differs (camelCase, PascalCase, UPPER CASE and even cases with separators like snake_case, dash-case, etc.). The "Preserve case" feature facilitates this. Just invoke "Preserve case" via the command palette (or define an own keybinding) and type in the new identifier.

图片 4

sed '{[/]<adr1>[,<adr2>][/]d' <fileName>  

Normalize and toggle region ends

When creating selections in Sublime, it can occur that the end of the selection comes before the beginning. This happens when you make the selection "backwards". To resolve this, you can normalize the regions with MultiEditUtils' normalize_region_ends command (default keybinding is ctrl/cmd+alt+n). When executing this command a second time, all regions will be reversed.

图片 5

This feature can also be very handy when you want to toggle the selection end of a single region.

图片 6


Jump to last region

When exiting multi selection mode, Sublime will set the cursor to the first region of your previous selection. This can be annoying if the regions were scattered throughout the current buffer and you want to continue your work at the last region. To avoid this, just execute MultiEditUtils' jump_to_last_region command (default keybinding is shift+esc) and the cursor will jump to the last region.

图片 7


Cycle through the regions

In case you want to double check your current selections, MultiEditUtils' cycle_through_regions command (default keybinding is ctrl/cmd+alt+c) will let you cycle through the active regions. This can come handy if the regions don't fit on one screen and you want to avoid scrolling through the whole file.

图片 8

n = line number

Strip selection

Sometimes selections contain surrounding whitespace which can get in the way of your editing. The strip_selection command strips the regions so that this whitespace gets removed. The default keybinding is ctrl/cmd+alt+s.

图片 9

string = string found in in line

Remove empty regions

When splitting your selection or performing other actions on your selection, it can happen that some regions are empty while others are not. Often only the non-empty regions are of interest. The remove_empty_regions commands will take care of this and remove all empty regions from your current selection. The default keybinding is ctrl/cmd+alt+r.

图片 10

regex = regular expression corresponding to the searched pattern

Quick Find All for multiple selections

Similar to the built-in "Quick Find All" functionality, MultiEditUtils provides a functionality which selects all occurrences of all active selections. The default keybinding of the multi_find_all command is ctrl+alt+f (on Mac it's cmd+alt+j).

图片 11

addr = address of a line (number or pattern )

Use selections as fields

Converts the selections to fields similar to the fields used in snippets. When the selection_fields command is executed, all current selections are saved as fields, which can be activated one by one. The first field is activated automatically. You can jump to the next field with tab (or the default keybinding) and to the previous field with shift+tab. If you jump behind the last field or press escape all fields will be converted to proper selections again. If you press shift+escape all fields will be removed and the current selection remains unchanged.

图片 12


You can bind this command to a keybinding by adding the following to your keymap (Change the key to the keybinding you prefer):

{ "keys": ["alt+d"], "command": "selection_fields" },

Although using one keybinding with the default options should be sufficient for most cases, additional modes and arguments are possible. Feel free to ignore or use them as you wish.


  • mode ("smart") is the executing mode, which defines the executed action. Possible modes are:
    • "push" to push the current selection as fields. This will overwrite already pushed fields.
    • "pop" to pop the pushed fields as selections
    • "remove" to remove the pushed fields without adding them to the selection. This has the same behavior as pop if only_other is true.
    • "add" to add the current selection to the pushed fields
    • "subtract" to subtract the current selection from the pushed fields
    • "smart" to try to detect whether to push, pop or jump to the next field
    • "toggle" to pop if fields are pushed, else push the selections as fields.
    • "cycle" to push or go next. This will cycle, i.e. go to the first if the last field is reached, never pops
  • jump_forward (true) can be true to jump forward and false to jump backward
  • only_other (false) ignores the current selection for pop and go next actions.

Suggestion for more keybindings based on the arguments:

// default use of selection_fields
{ "keys": ["alt+d"], "command": "selection_fields" },
// add the current selections as a fields
{ "keys": ["alt+a"], "command": "selection_fields", "args": {"mode": "add"} },
// jump and remove current selection in selection_fields
{ "keys": ["ctrl+alt+d"], "command": "selection_fields",
  "args": {"mode": "smart", "only_other": true} },
// cancel selection_fields and remove current selection
{ "keys": ["ctrl+alt+shift+d"], "command": "selection_fields",
  "args": {"mode": "toggle", "only_other": true} },

d = delete


Either use Package Control and search for MultiEditUtils or clone this repository into Sublime Text "Packages" directory.



MIT © Philipp Otto



Remove the 3rd line:


sed '3d' fileName.txt  


Remove the line containing the string "awk":


sed '/awk/d' filename.txt  


Remove the last line:


sed '$d' filename.txt  


Remove all empty lines:


sed '/^$/d' filename.txt         

sed '/./!d' filename.txt  


Remove the line matching by a regular expression (by eliminating one containing digital characters, at least 1 digit, located at the end of the line):


sed '/[0-9/][0-9]*$/d' filename.txt  


Remove the interval between lines 7 and 9:


sed '7,9d' filename.txt  


The same operation as above but replacing the address with parameters:


sed '/-Start/,/-End/d' filename.txt  


The above examples are only changed at the display of the file (stdout1= screen). 


For permanent changes to the old versions (<4) use a temporary file for GNU sed using the "-i[suffix]":


sed -i".bak" '3d' filename.txt  


Task: Remove blank lines using sed

Type the following command:


$ sed '/^$/d' input.txt > output.txt  


Task: Remove blank lines using grep


$ grep -v '^$' input.txt > output.txt  


Both grep and sed use special pattern ^$ that matchs the blank lines. Grep -v option means print all lines except blank line.


Let us say directory /home/me/data/*.txt has all text file. Use following for loop (shell script) to remove all blank lines from all files stored in /home/me/data directory:




for i in $files  


  sed '/^$/d' $i > $i.out  

  mv  $i.out $i  



Updated for accuracy.

- Delete one or more lines from a file Syntax: C代码 sed {[/]n|string|regex[/]}d fileName sed {[/]adr1[,adr2][/]d fileName /.../=delimiters n = line number string = string fo...


上一篇:文本自定义,CSS如何设置选中文本的颜色 下一篇:memcache的作用及安装,linux下memcached的安装教程