PDA

View Full Version : How to write something on a text file on a certain line?



leon.vitanos
December 17th, 2010, 07:57 PM
This is how i write something on text file:


{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... ?

roccivic
December 17th, 2010, 08:22 PM
You could append:

{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.

leon.vitanos
December 17th, 2010, 08:28 PM
You could append:

{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?

hakermania
December 17th, 2010, 09:21 PM
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:~$

Some Penguin
December 17th, 2010, 09:46 PM
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).

leon.vitanos
December 17th, 2010, 10:01 PM
thanx it works :)