Deleting a Specific Line From a Text File in Command Line in Linux

On Linux, how to delete a specific line from a text file in command line? For example, to delete the 4th line from a file

aaa
bbb
ccc
ddd
eee
ffffff

You can use the “stream editor for filtering and transforming text” sed.

With GNU sed:

sed -i '4d' ./file

Here, -i means edit the file inplace. d is the command to “delete the pattern space; immediately start next cycle”. 4 means the 4th line.

The file content will be:

aaa
bbb
ccc
eee
ffffff

There are more combinations for deleting lines. Some examples are:

Remove the last line:

sed '$d' filename.txt

Remove all empty lines:

sed '/^$/d' ./file

or

sed '/./!d' ./file

Remove lines from 7 to 9:

sed '7,9d' ./file

Remove the line matching by a regular expression REGULAR:

sed '/REGULAR/d' ./file

For a simple example, remove the lines containing “oops”:

sed '/oops/d' ./file

Similar Posts

  • Patching with git

    Tutorials on how to create patches and apply patches with git. A nice tutorial: https://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/ Manuals: git format-patch: https://www.systutorials.com/docs/linux/man/1-git-format-patch/git apply: https://www.systutorials.com/docs/linux/man/1-git-apply/ Read more: How to create a git branch on remote git server How to do diff like `git diff –word-diff` without git on Linux? Cheatsheet: Git Branching with a Git Server What about the…

  • How to import a googlecode git project to github project

    I wanna migrate leveldb (code.google.com/p/leveldb) to my github repository. Create a new repo on github.com with this link https://help.github.com/articles/creating-a-new-repository git clone https://code.google.com/p/ project-name.git Set the remote origin url as follows: git remote set-url origin https://github.com/ username/ repo-name.git git pull to pull the latest. git push origin master after making local changes. This is the original…

  • Good free images hosting services on the Web?

    Suggestions on some good free images hosting services on the Web? Two good free image hosting websites: Imgur: http://imgur.com/ Postimage.org: http://postimage.org/ Read more: Shared hosting services with SSH enabled Free server images – SysTutorials QA Where to search and download free images? Are there good free CDNs on the Web How to get a free…

  • How to convert between Simplified Chinese and Traditional Chinese characters in text files on Linux?

    How to convert between Simplified Chinese and Traditional Chinese characters in text files on Linux from command line? You can use opencc to convert between Traditional Chinese and Simplified Chinese: https://github.com/BYVoid/OpenCC For example, to transfer a file in simplified Chinese sc.txt to traditional Chinese: opencc -i sc.txt -o tc.txt -c zhs2zht.ini The authors also provide…

  • Chinese Charactor Configuration on Fedora 11

    最新的更新版本请看: Fedora 中文字体设置. 使用Linux时我个人倾向使用英文环境系统,而Fedora11在英文环境下中文字体有时会不太好看,经常遇到需要字体优化美化的问题。 以下是我的配置方案,经测试效果还算不错,解决了Fedora 11 中文字体难看的问题: 方案1:使用uming和ukai字体,即AR PL UMing CN等。 关键是使用的字体包如下: 首先要安装这两个字体: cjkuni-ukai-fonts cjkuni-uming-fonts 然后配置一下~/.fonts.conf文件. 使sans-serif serif monospace字体中文使用uming/ukai即可. 我的.fonts.conf文件可以从这里下载(两种选择, 我喜欢前者): https://github.com/zma/config_files 使用Liberation和uming/ukai字体: .fonts.cofn.liberation 使用dejavu和uming/ukai字体: .fonts.conf.dejavu 下载后放到自己的$HOME下改名为.fonts.conf就可以了。 使用uming字体效果如下(请放大后看效果): 方案2:安装文泉驿字体,这个非常简单,安装相应包即可了。 如果喜欢其它的字体选择性的安装上就可以了,只要注意只安装自己需要的就行了。有人使用微软雅黑字体,首先这是侵权的,其次开源的字体做得其实已经很不错了。 最后将字体平滑选项打开, KDE和gnome都有相关设置方法。 以上内容只是针对使用xft字体系统的设置。对于使用核心字体系统的X程序来说字体依然会出现很丑的情况。 下面是针对emacs的设置方法: 首先需要安装这个字体包: xorg-x11-fonts-misc 注意到在中文系统下emacs的中文显示非常好,而在英文环境中去非常差,我们可以利用这一点,在运行emacs前首先将系统环境设为中文即可。 在~/bin/下建立一文件ema 内容如下: #!/bin/bash rm -f ~/.emacs ln -s ~/.emacs.x ~/.emacs LANG=zh_CN.UTF-8 emacs –fullheight -r $* 然后加入执行权限即可: chmod +x…

3 Comments

  1. This was very helpful.
    Question.
    Once we have the offending line removed, how do we (using the command line) overwrite the file with the new, corrected version?

    For example…
    sed ‘//d’ file.txt
    removes the html expression but now I want to save this version to the same file.

    Thank you for any suggestions.

  2. Greetings. I found the answer. Insert -i after sed command.

    sed -i ‘//d’ file.txt
    overwrites the file with the corrected information.

    But

    What if the expression to remove contains a forward slash character. Haven’t figured out how to make this work.

    sed -i ‘//d’ file.txt

    There must be a way to escape the / character in the expression to remove. I’ve tried surrounding it with ‘ and \. Nether seem to be the solution.

    1. Hai we can delete a particular line in text file using sed command. But again how to recover that deleted text in that filename after deleting if by a mistake?

Leave a Reply

Your email address will not be published. Required fields are marked *