Help with file editing in Linux? (Without a text editor)

Discussion in 'Programming/Scripts' started by dan4ielo, Jun 4, 2021.

  1. dan4ielo

    dan4ielo New Member

    Hi all,
    I am currently trying to edit a conf file within a docker container, however, the people that have made it have not preinstalled any text editors with it. When I get into the container, I am logged in as root, however, apt does not find any packages from the typical repositories. So here is the dilema. I need to add a couple of IPs within the file (each on a separate line), but can't find a way to do it. I thought that using the echo "ip_address" >> file_name.php would work, but I don't know if it is possible to specify the row on which the IP should be added.

    Here is the snipped from the file I am talking about:

    'trusted_domains' =>
    array (
    0 => '',
  2. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    Vi is normally available but I am not used to it. Nano is easier but it may not be installed. Anyway, try installing nano or learn to use vi.
  3. Jesse Norell

    Jesse Norell Well-Known Member Staff Member Howtoforge Staff

    I don't use docker, but I suspect the correct way to edit docker config files is from "outside" the container, to be put in place when it starts. Who knows, I could be way off there.

    You could use sed or awk, or even head and tail. Some web searches will find many examples, eg. this page shows all three of those and more.
    ahrasis likes this.
  4. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Make sure there really is no text editor. Try commands
    If there are no editors, I suspect what @Jesse Norell wrote is true.
    You are correct that >> appends to the end of the file, so you can add new last line, but not in the middle. You could get creative with commands head and tail and snip each line to it's own file, then combine them to one (with cat for example). There is also cut to get parts of a line.
    If you have terminal where you can cut and paste, edit on your workstation editor and paste back.
    ahrasis likes this.
  5. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    I normally want to know what inside my conf file before editing them (cat), back them up (cp), use proper editor and won't take a risk of overriding it with such commands. In any event, I found this topic which teach on how to install app inside docker: How to Install, Run and Delete Applications Inside Docker Containers. I hope it is useful to you.
  6. dan4ielo

    dan4ielo New Member

    Thank you for the answers, everyone. To be honest, the answer was extremely simple :-D. It turns out the repositories are not updated inside the container, so in order to install an editor I had to update the repositories with apt update. Though it took me 5 hours to find that in a random video on YouTube ... ( I feel so dumb). Anyway, on topic, I tried using the traditional GNU commands (cat, cp, head, tail, etc.) but either I am not creative enough or don't know them well enough. Can anyone give an example of the way they can be used to achieve the goal?
  7. Jesse Norell

    Jesse Norell Well-Known Member Staff Member Howtoforge Staff

    There's an example in the link I posted above.
  8. nhybgtvfr

    nhybgtvfr Well-Known Member HowtoForge Supporter

    sed '# i text string to be inserted' file_name.php

    # is the line number you want the text inserted on.
    i is the instruction to insert text.

    so to insert an ip and port number on line 15 of the file connection.txt:

    sed '15 i' connection.txt

Share This Page