Results 1 to 6 of 6

Thread: How to write something on a text file on a certain line?

  1. #1
    Join Date
    Nov 2010
    Beans
    69

    Talking How to write something on a text file on a certain line?

    This is how i write something on text file:

    Code:
    {system("echo \"Hello\" > /home/$USER/.config/Wallch/Checks/language_selection");

    But how to write something on a certain line without changing the others?
    For example how can i write "Hello" on the third line of my text file without changing the first line... ?

  2. #2
    Join Date
    Apr 2008
    Location
    Ireland
    Beans
    286
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: How to write something on a text file on a certain line?

    You could append:
    Code:
    {system("echo \"Hello\" >> /home/$USER/.config/Wallch/Checks/language_selection");
    Otherwise, if you want your text inserted at a specific line, you'll have to read in the whole file, then add your changes and finally overwrite the original file.

  3. #3
    Join Date
    Nov 2010
    Beans
    69

    Re: How to write something on a text file on a certain line?

    Quote Originally Posted by roccivic View Post
    You could append:
    Code:
    {system("echo \"Hello\" >> /home/$USER/.config/Wallch/Checks/language_selection");
    Otherwise, if you want your text inserted at a specific line, you'll have to read in the whole file, then add your changes and finally overwrite the original file.
    The exactly thing someone had answered me.. but this will not go to the certain line that i want but on a new line... Again: For example how can i write "Hello" on the third line of my text file without changing the first line... ? This is what i want... Any ideas?

  4. #4
    hakermania's Avatar
    hakermania is offline Τώρα ξέρεις τι γράφω εδώ!
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    1,702
    Distro
    Ubuntu Development Release

    Re: How to write something on a text file on a certain line?

    Code:
    alex@MaD-pc:~$ cat lol
    1
    2
    Hello
    Hello
    3
    4
    
    alex@MaD-pc:~$ sed -i "5i\Hello" lol
    alex@MaD-pc:~$ cat lol
    1
    2
    Hello
    Hello
    Hello
    3
    4
    
    alex@MaD-pc:~$
    Website

    Wallch (Wallpaper Changer): Sourceforge | Launchpad

  5. #5
    Join Date
    Nov 2009
    Beans
    1,081

    Re: How to write something on a text file on a certain line?

    Files don't have lines; they have bytes. Therefore, you have to rewrite the file (or implement a custom filesystem which is meant for edits like this).

  6. #6
    Join Date
    Nov 2010
    Beans
    69

    Re: How to write something on a text file on a certain line?

    thanx it works

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •