How to delete specific lines from a file using sed

On certain occasions we need to delete a specific line from a file or several, for example, it has happened to me that I have had a whole list of files and I need to delete line # 27 of all these (line # 27 is that of an ACL , norm, rule, configuration), I can either edit file by file or I can just achieve what I need using the thirst and a bash script (optional).

But, let's try a single file somewhat simple.

We have the file distros-deb.txt which contains this:

debian

kubuntu

archlinux

soluses

mint

In other words, the file distros-deb.txt is in which we will put the Debian-based distros, but there we see that in line # 3 there is "archlinux", a distro that obviously has nothing to do with Debian, so we must eliminate that line. To eliminate line # 3 of that file we will put the following:

sed "3d" distros-deb.txt > distros-deb-ok.txt

Explaining this line is somewhat easy, with thirsty "3d" we are indicating that we will delete line # 3, with distros-deb.txt We indicate what file to work on, that is, delete line # 3 of this file, up to here if we press Enter it will show us what we want but in the terminal, so > distros-deb-ok.txt we are indicating that instead of showing the result in terminal, that it put it in a file with this name.

What is simple?

Also, we can avoid using the > distros-deb-ok.txt using a proper parameter of thirst, the parameter -i

That is, in case we want to delete the line from the file and save it with the same name (and not in another file), simply add the parameter -i :

sed -i "3d" distros-deb.txt

This will remove line # 3 from distros-deb.txt and save it.

What if I want a range of lines, that is to remove line # 3 but also # 4 and # 5? To achieve this we put the range from 3 to 5, that is:

sed -i "3,5d" distros-deb.txt

And it will show me only debian and kubuntu 😀

So what if I want to delete from line 2 to the last one, when I don't know the total lines?

Just use the dollar symbol - »$

sed -i "2,$d" distros-deb.txt

In case you want to eliminate from the first line to # 4 then we simply put the value 1 at the beginning:

sed -i "1,4d" distros-deb.txt

This has been everything, a very useful tip when you want to make bash scripts to automate tasks and you need to modify and eliminate lines of configuration files, to modify we can use thirst o perl, as well as to eliminate we already know how to do it with sed 😉

regards


Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   mss-devel said

    Very good contribution 😉

    1.    KZKG ^ Gaara said

      Thank you

      By the way, we received your email right now I answer you 😀

      regards

  2.   nameless said

    As high priest of the terminal, servers and ssh connections I come to you, oh great KZKG ^ Gaara, and I ask you: where can I get a tutorial at the level of my ignorance that allows me to use ssh connections between two remote machines on different networks to share text files, pdf, image and sound (mp3)….

    : )

    Seriously, could you guide me in this regard, I have two machines, one at work and one at home and I need an ssh connection between them (because as I understand it, ssh allows content to be shared between machines, am I right?).
    And if I'm wrong, what application do you recommend?
    And where do I find a basic tutorial in this regard?

    1.    -spyker- said

      scp

      scp user @ machine_address: path user @ machine_address: path.

      Same syntax as cp, source -> destination.

  3.   f3niX said

    You showed up man, you were lost.

  4.   Joaquin said

    Good Tip!

  5.   LycusHackerEmo said

    Interesting tip… xD

    Do you happen to know one that makes bold text stand out?
    I mean, I have a txt file that is a dictionary, it has more than 10000 lines and I want it to highlight certain text before the ":" suspension points and doing it one by one is too much.

    1.    KZKG ^ Gaara said

      Hello,

      A txt file is plain text, as its name implies ... plain, without formats or anything similar, I'm sorry but I think that what you ask cannot be done, can it? 🙁

      regards

      1.    to that said

        actually it can, but you would have to know the destination format.
        eg:
        echo $ (echo "Robert: Hello. Change here" | sed 's / \ ./. \\ e [40; 31m /; s / \: /: \\ e [40; 35m /')
        it's a matter of coping.
        the other way to delete that can be used is sed '/' $ 1 '/ d' but you have to be sure of the re.

        1.    LycusHackerEmo said

          then finish saving it in * .odt

          Isn't there an easier way to do it with LibreOffice?

  6.   Lolo said

    Could you delete part of a line and leave the rest?

    Let's say I want to delete everything in front of a word in a certain row.

    Or delete everything that follows that word.

    1.    to that said

      Yes, it's a matter of pulling a regex (if necessary man sed -r, –regexp-extended)
      Starting from what I find
      echo «Robert: Hello. Change here »| sed 's / Change //'
      with a well-defined pattern and with. (one character) and * (more than one)
      After:
      echo «Robert: Hello. Change here »| sed 's / Change. * //'
      Before:
      echo «Robert: Hello. Change here »| sed 's /. * Change //'
      If it matters that the word appears
      echo «Robert: Hello. Change here »| sed 's / Change. * / Change /'
      or more elaborate
      in the line containing Robert what goes after Change
      echo -e «Fritz: Hello. Change here \ nRobert: Hello. Change here »| sed '/Robert/s/Cambio.*//'
      or like at the beginning take out the second line and process the rest
      echo -e «Fritz: Hello. Change here \ nRobert: Hello. Change here \ nOther »| sed -e 2d -e 's / Change. * //'
      echo -e «Fritz: Hello. Change here \ nRobert: Hello. Change here \ nOther »| sed '2d; s / Change. * //'

      1.    Lolo said

        Thanks, it is very useful to me.

  7.   msx said

    Nice article, of which I like, how great the SysAdmin!
    What would our life be without sed, awek, perl, grep, tail, head, "Emacs" and so many other essential tools !?

  8.   Lisbeth Ollarves said

    Thanks, it was very helpful.

  9.   Pins said

    Hello, and how could you delete lines 1,4 and 10 from a file in the same command?