ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi , i already went all over the net testing examples with sed and awk and none of them worked .
writing on line 4 this : "test" with sed over temp.tmp , did not worked .
Quote:
sed -i '4i\test' temp.tmp
One of the things i must hate on searching the web for stuff , is that people like to complicate things for a simple thing .
Others even use programming languages to give an example for a simple thing like this , like using perl to write a simple text on file , even if the user is not using perl .
Note : today is a bad day for me , too much coffee .
Please , if you are going to write here , then do it simple , do not complicate things just to show others that you know .
It is really unclear what you are tryng to accomplish from your description.
It would help if you could provide a clear example of what you are trying to do, including an example of the temp.tmp file before being changed, and the expected result after being changed.
Your post title, Writing data on to specific line in file and the code example you gave suggest that you want to write the word "test" onto line four of the file, temp.tmp. You do not say whether you want to append to line 4, insert a new line 4 or replace line 4.
Please tell us how it is failing to do what you want. Saying, "did not worked", is not helpful to anyone trying to help.
Help us help you by supplying the necessary details.
Last edited by astrogeek; 03-11-2017 at 04:43 PM.
Reason: typo(s)
well , i think i told everything on the first post , but if you want things more clear then here it is :
How to write specific text on a particular line in a file ?
if i wanted to write on first line using "echo" , i would do :
if i want to write on last line of that file with "echo" i would do :
What i want to know is how do i write some text to a particular line number .
We are happy to try to help you, but we need to know what you're stuck on. One sed example with admonitions not to give you an answer you don't want, doesn't really tell us much.
...with examples that are easy to follow. Also note that things like this:
Quote:
Originally Posted by pedropt
One of the things i must hate on searching the web for stuff , is that people like to complicate things for a simple thing. Others even use programming languages to give an example for a simple thing like this , like using perl to write a simple text on file , even if the user is not using perl.
Note:today is a bad day for me, too much coffee.
Please , if you are going to write here , then do it simple , do not complicate things just to show others that you know
When asking for help, telling people exactly HOW you will accept that help isn't polite. And when you ask a question like "how to replace text on a certain line", and they tell you "well, you can use Perl like xxx", they have gone out of their way to answer your question in a way that works...complaining that it's 'complicated', or not what you want, is plain rude. If you don't say "I need to do this in sed", then don't complain about the answers that use bash or something you don't want.
And in your example in your original post, if you had the syntax right, it would have worked. Did you try
Please provide the information I mentioned in my first post. Specifically:
* What is in the file temp.tmp before you write to it?
* What do you expect it to contain after you write to it?
* Do you want to insert a new line, or modify or replace an existing line?
* How exactly is your example code failing to provide the expected results?
This...
Code:
echo "test" > temp.tmp
Does not only write to the first line of the file, it removes anything and everything that may be in the file before hand - which may or may not be the intended result!
Please try to fully describe what you are trying to do in order to help us provide help that applies to your exact problem.
on the example he gave these commands to write with sed on line 33 with the text "anything":
Quote:
sed -i '33ianything' textpath
or
sed -i '33i\anything' textpath
i assume that (textpath) on the command is the location of file you want to write ,
but this command does not work for me .
using this particular command to my needs , it would be something like this :
Quote:
sed -i '33i\test' temp.tmp
where 33 is the line where i want to write , where (test) is the text i want to write and where temp.tmp is the file where i want that data to be written .
Then i looked this next link and i lost my patience when someone used perl to do that command :
Loose my patience because if the user need to install perl specifically just to write some text on a file , witch is ridiculous .
However , today is not one of my best days .
where on my case i just need to write on a single file , so then i tested with awk :
Quote:
awk 'NR==3{print "test"}1' temp.tmp
I am pretty sure that what i need is very simple , but sometimes the most simples things in bash turn out to be an incredible headache just because of a missing symbol in the command .
It does not matter witch line it is , i just need a command to write some text on a specific line that i will choose later , and i need that command because i will write multiple things on that specific file from multiple routines on the script i am building .
Loose [Lose] my patience because if the user need to install perl specifically just to write some text on a file , witch is ridiculous .
Sorry, but if you looked at your system you would already find a recent version of perl. No need to install anything or lose patience. Perl is ubiquitous.
Speaking of looking, if you look at the manual page for sed on your system you will see that i is the command to insert text.
Code:
man sed
So you would use it like this in plain sed to insert "foo" as the fourth line in a file, pushing all the other lines down.
Code:
sed -e '4ifoo' temp.temp > new.temp.temp
You can do in-place editing if your version of sed allows it.
thanks for the reply , but that example you posted applies and at sametime does not apply to me .
Are we supposed to guess why?
Please tell us why it does, and does not apply to your case.
All the history of your previous searches is irrelevant unless you explain, clearly, why they do not apply to your case.
Quote:
Originally Posted by pedropt
I am pretty sure that what i need is very simple , but sometimes the most simples things in bash turn out to be an incredible headache just because of a missing symbol in the command .
It does not matter witch line it is , i just need a command to write some text on a specific line that i will choose later , and i need that command because i will write multiple things on that specific file from multiple routines on the script i am building .
Which provides no useful information for us to understand what you want to accomplish.
You have not answered my own direct questions above, which would tell us whether you want to...
* Insert a new line with each write
* Modify the existing line with each write
* Replace the existing line with each write
Until you answer this very clear, basic request there is little point in continuing the discussion.
pedropt, the reason we ask questions is so that we can understand what your requirements are and offer solutions based on that knowledge. Getting help is an interactive process. Unless you interact with those trying to help by responding to their questions, you only waste your time and the time of everyone else.
It appears from your post #9 that you are attempting to write to a particular line of an empty file:
Code:
temp.tmp file continues empty after the command.
So here we are at post #12 and only now discover, only obliquely, that the file is empty before attempting to write to it.
In post #2, the very first reply to your question, the very first thing that you were asked was the state of that file before and what was expected after writing to it...
Quote:
It would help if you could provide a clear example of what you are trying to do, including an example of the temp.tmp file before being changed, and the expected result after being changed.
Your post title, Writing data on to specific line in file and the code example you gave suggest that you want to write the word "test" onto line four of the file, temp.tmp. You do not say whether you want to append to line 4, insert a new line 4 or replace line 4.
Please tell us how it is failing to do what you want. Saying, "did not worked", is not helpful to anyone trying to help.
Had you responded to any of the underlined questions, which you still have not done, people here would have understood the problem and been able to provide replies directly applicable to solving your problem.
If you are trying to directly write to a specific line of an empty file using sed, then you are attempting the impossible!
An empty file does not contain numbered lines just waiting to be written! Lines in a file are numbered by counting the line breaks, or newline characters. An empty file by definition has none, so there is no line 1 or 4 or 33 or anything else!
Sed and awk actions are triggered by the lines of a file. In order to perform some action on a given line number in a file with these tools, that line must exist! So none of the sed or awk, and most other tools, will work like that.
If you really want to write to some line, say line 4, of an empty file then you must create the preceding lines first.
So, to speculate on the answers to questions in post #2, and reply based on those...
* The file temp.tmp is empty to start with
* After writing to it you expect 3 empty lines followed by one line with the word "test" in it
* Your attempts are failing because the file remains empty at the end
The reason your example failed is because the file is empty to start, so sed cannot write to non-existent line 4!
Here is one very simple way to do that in this specific case (but not in general for writing additional lines).
Code:
echo -e "\n\n\ntest" >temp.tmp
cat temp.tmp
empty line
empty line
empty line
test
In future, try to provide all relevant details when asking your questions, and show consideration to those willing to help by responding to their questions and making effort to understand and apply their comments and suggestions. Everyone here volunteers their time, please respect their time and efforts by helping them help you.
If you are unwilling or unable to ask questions in a manner that allows us to help you, it's unlikely our community will be able to provide you a solution. Unfortunately, serial offenders who show wanton disregard for this request after multiple pointers may be asked to seek help elsewhere. We truly hope that isn't necessary, and assure you Linux and Open Source are extremely rewarding and well worth the learning curve in the long run.
Please make the effort and become a valued member of LQ!
Yes the file is empty and there is no line 4 on it .
temp.tmp file only have 1 empty line , is the 1st line .
However , sed does also not write on the 1st line .
Quote:
echo -e "\n\n\ntest" >temp.tmp
cat temp.tmp
works , but what echo does is to create 3 empty lines first and then on the 4th line writes "test" .
So , for what i could understand , you guys are telling me that for sed to work i have to have something written on the first line and the line where i want to write (ex:23) with sed must already exists even if it is empty ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.