LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2017, 03:48 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
Writing data on to specific line in file


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 .
 
Old 03-11-2017, 04:40 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to the Programming forum.

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.

Please see the Site FAQ, and in particular the link at bottom of that page, How To Ask Questions The Smart Way for guidance in forming your questions.

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)
 
Old 03-11-2017, 05:55 PM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 :
Quote:
echo "test" > temp.tmp
if i want to write on last line of that file with "echo" i would do :

Quote:
echo "test" >> temp.tmp

What i want to know is how do i write some text to a particular line number .

Last edited by pedropt; 03-11-2017 at 05:57 PM.
 
Old 03-11-2017, 06:30 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by pedropt View Post
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.

Putting "how to write to specific line in a text file on linux" into Google yields lots...and a ton from this site:
http://www.linuxquestions.org/questi...t-file-664654/

...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
Code:
sed -i "4i test" temp.tmp

Last edited by TB0ne; 03-11-2017 at 06:38 PM.
 
1 members found this post helpful.
Old 03-11-2017, 06:32 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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.
 
Old 03-11-2017, 06:57 PM   #6
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
thanks for the reply , but that example you posted applies and at sametime does not apply to me .

First link i looked on the web was this one from stack exchange on post 2 :
http://unix.stackexchange.com/questi...in-a-text-file

witch is what i need .

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 .

Then i went to this link :
http://unix.stackexchange.com/questi...ines-of-a-file

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 .

Last edited by pedropt; 03-11-2017 at 07:01 PM.
 
Old 03-11-2017, 07:30 PM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by pedropt View Post
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.

Code:
sed -i -e '4ifoo' temp.temp
 
Old 03-11-2017, 07:33 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
What you are doing is inserting a (new) line at the location you specified. You want the "s" command, not "i\". To replace an entire line try this
Code:
sed -i '33 s/.*/test/' temp.tmp
Simple, as requested.

Last edited by syg00; 03-11-2017 at 07:34 PM.
 
1 members found this post helpful.
Old 03-11-2017, 07:47 PM   #9
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Sorry but :
Quote:
sed -i '33 s/.*/test/' temp.tmp
does not work around here .

i tried to write on 1st line by changing the command and i did not had output on the file :
Quote:
sed -i '1 s/.*/test/' temp.tmp
temp.tmp file continues empty after the command .
 
Old 03-11-2017, 07:51 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Put something in it first.
 
3 members found this post helpful.
Old 03-11-2017, 07:52 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by pedropt View Post
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 View Post
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.
 
Old 03-11-2017, 07:57 PM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by pedropt View Post
i tried to write on 1st line by changing the command and i did not had output on the file :
Code:
sed -i '1 s/.*/test/' temp.tmp
temp.tmp file continues empty after the command .
That makes sense. If the file was empty, there is no "line 1" to trigger the action.
 
1 members found this post helpful.
Old 03-12-2017, 12:44 AM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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.

Please see the Site FAQ, and in particular the link at bottom of that page, How To Ask Questions The Smart Way for guidance in forming your questions.

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
Please review, again, the Site FAQ, Site Rules.

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.

From the Site FAQ...

Quote:
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!
 
Old 03-12-2017, 05:18 AM   #14
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Thanks for the reply .

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 ?
 
Old 03-12-2017, 05:26 AM   #15
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
The tests i did now is that if write with echo :
Quote:
echo -e "\n\n\ntest" >temp.tmp
and then if i write with sed :

Quote:
sed -i '3 s/.*/yup/' temp.tmp
it works perfectly .

but if i write :
Quote:
sed -i '5 s/.*/abcd/' temp.tmp
then it does not work , because line 5 does not exist yet.

Honestly , i don't think many people know that sed is not able to create a new line to write the output .

Resuming :
Sed only writes the data if line exists even if it is empty .
Sed is not able to create new lines on a file to write the output .

Last edited by pedropt; 03-12-2017 at 05:28 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed add line under a specific line in a file aristosv Linux - Software 4 08-30-2015 02:34 AM
[SOLVED] Filter through line/s to grab specific fields/data in the line with example shayno90 Linux - Newbie 11 10-14-2009 11:51 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
vb.net writing to a specific line in a text file mrobertson Programming 3 12-30-2005 10:24 PM
Writing to a specific line in a text file mrobertson Programming 6 12-30-2005 04:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration