insert lines into a file after calculating where to insert
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.
insert lines into a file after calculating where to insert
I am trying to find a way to insert lines without using temp files through a shell script.
what I am attempting to do is create lines of text dynamically and then insert them into another file just before the end of the file. I know that no matter the line count the new lines will always go 2 lines before the end of the file.
so if the file has 16 lines I need to insert at line 14.
if the file is 22 lines I need to insert at line 20.
I can get the line count but not for sure how to apply in in the shell script or how to insert the lines at the required location.
was looking at
wc -l /home/myfile | awk '{print $1}'
to get the line count but unclear on how to use the $1 to remove 2 lines and then the insert lines piece.
I am trying to find a way to insert lines without using temp files through a shell script.
what I am attempting to do is create lines of text dynamically and then insert them into another file just before the end of the file. I know that no matter the line count the new lines will always go 2 lines before the end of the file.
so if the file has 16 lines I need to insert at line 14.
if the file is 22 lines I need to insert at line 20.
I can get the line count but not for sure how to apply in in the shell script or how to insert the lines at the required location.
was looking at
wc -l /home/myfile | awk '{print $1}'
to get the line count but unclear on how to use the $1 to remove 2 lines and then the insert lines piece.
There is no insertion into file, there is only writing at a given position.
And, for that matter, reading from a given position.
So, if you want to make your life complex, open a file in RW mode, find the position at which to insert, first save the file tail, then insert and append the saved file tail.
I doubt you can do this from shell.
And I do not understand why you want to make your life so complex.
I am trying to modify another script that is ran in a batch file. So after this script is ran I need to insert a few more commands to check for changes for this new setup. Right now I have to modify the second script by hand after running this one, that is why I am trying to find a way to insert the lines just above the exit command in the second script.
Here is what I have found and trying to piece together.
# get line count - not for sure how to assign the $1 to a variable or if I can use it here
wc -l /home/myfile | awk '{print $1}'
# this will supposedly do the insert for me one I have the linenumber and place it where the 13 is.
# awk ' { print $0; if (NR%13==0) {print "inserted line"}}' file
maybe something like this
$str1 = "My text to insert";
$str2 = "more text to insert";
$myfile = "/home/myfile";
awk 'q{
# if "q" has value, print q
print q
}
{
q=p # save the previous line
p=$0 # save current line
print "p is " p
print "q is " q
}
END{
print "insert"; print q; print p
}' file > temp
mv temp file
Last edited by ghostdog74; 12-23-2009 at 10:23 PM.
I have been looking over some of your examples on your site, and they are bit more easier to understand kind of. Was looking at this one "Insert Text at a specific block" But I am still unclear about this post.
Could you explain what is taking place in the awk statement you posted?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.