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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
12-23-2009, 11:21 AM
|
#1
|
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Rep:
|
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.
|
|
|
|
12-23-2009, 11:33 AM
|
#2
|
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by xonar
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.
|
|
|
|
12-23-2009, 12:09 PM
|
#3
|
|
Member
Registered: Dec 2009
Distribution: Slackware
Posts: 147
Rep:
|
You've already figured a way to get the line number.
Is the following what you're asking to do ???
To insert before the given line:
Code:
sed "${LineNumber}i\Place_your_text_here" FILE > FILE.new
To append after the given line:
Code:
sed "${LineNumber}a\Place_your_text_here" FILE > FILE.new
To remove from that line through the end of the file
Code:
sed "${LineNUmber},\$d" FILE > FILE.new
and then use mv to rename the files accordingly.
--ET
|
|
|
|
12-23-2009, 12:14 PM
|
#4
|
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Original Poster
Rep:
|
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.
But your right about complicating it.
|
|
|
|
12-23-2009, 12:16 PM
|
#5
|
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Original Poster
Rep:
|
I am not for sure how to use the {print $1} in the script so it is assigned and then subtract 2 from the line count.
Thanks for the insert, I will be giving that a try now.
|
|
|
|
12-23-2009, 12:28 PM
|
#6
|
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Original Poster
Rep:
|
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";
# insert line 1
$1 = wc -l /home/myfile | awk
$1 = $1 - 1
awk ' { print $0; if (NR%$1==0) {print $str1}}' $myfile
# insert line 2
$1 = wc -l /home/myfile | awk
$1 = $1 - 1
awk ' { print $0; if (NR%$1==0) {print $str2}}' $myfile
Last edited by xonar; 12-23-2009 at 12:33 PM.
Reason: to give my thought on this piece
|
|
|
|
12-23-2009, 12:59 PM
|
#7
|
|
Member
Registered: Dec 2009
Distribution: Slackware
Posts: 147
Rep:
|
Quote:
Originally Posted by xonar
I am not for sure how to use the {print $1} in the script so it is assigned and then subtract 2 from the line count.
Thanks for the insert, I will be giving that a try now.
|
Something like this:
Code:
LineNumber=$(wc -l FILE |awk '{print $1}')
let NewLineNum=$LineNumber-2
--ET
|
|
|
1 members found this post helpful.
|
12-23-2009, 07:16 PM
|
#8
|
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
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.
|
|
|
1 members found this post helpful.
|
12-23-2009, 10:09 PM
|
#9
|
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Original Poster
Rep:
|
ghostdog74:
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?
|
|
|
|
12-23-2009, 10:25 PM
|
#10
|
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
run the script with print statements and see the output for yourself.
|
|
|
|
12-24-2009, 12:08 AM
|
#11
|
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Original Poster
Rep:
|
Ok, I got it now. That works great thank you again.
I have a small sample worked up that is working great for me. I did go ahead and work up a sample using the line count method also.
Thanks to everyone and here is the sample script of both methods. Maybe this will help others out also.
Input File
Quote:
# Generated by NetworkManager
nameserver 10.0.0.1
nameserver 10.0.0.2
nameserver 10.0.0.3
nameserver 10.0.0.4
nameserver 10.0.0.5
nameserver 10.0.0.6
nameserver 10.0.0.7
nameserver 10.0.0.8
nameserver 10.0.0.9
nameserver 10.0.0.10
nameserver 10.0.0.11
nameserver 10.0.0.12
|
Method 1, inserts the new line of text just after the first line in a file.
Quote:
#!/bin/sh
myfile=/home/mytestfile.conf
mytemp=/home/myTempTestFile.conf
awk 'q{
# if "q" has value, print q
print q
}
{
q=p # save the previous line
p=$0 # save current line
}
END{
print "insert";
print q;
print p;
}' $myfile > $mytemp
mv $mytemp $myfile
exit 0
|
Method 2, inserts the new line of text at a given line within the file by getting the line count.
Quote:
#!/bin/sh
myfile=/home/mytestfile.conf
mytemp=/home/myTempTestFile.conf
LineNumber=$(wc -l $myfile |awk '{print $1}')
NewLineNum=$LineNumber-4 # insert 5 lines from the end
awk 'NR=='$NewLineNum'{print "insert new line"; print;next}1' $myfile > $mytemp2
mv $mytemp $myfile
exit 0
|
Method 1 Output - exactly 2 lines from the end
Quote:
# Generated by NetworkManager
nameserver 10.0.0.1
nameserver 10.0.0.2
nameserver 10.0.0.3
nameserver 10.0.0.4
nameserver 10.0.0.5
nameserver 10.0.0.6
nameserver 10.0.0.7
nameserver 10.0.0.8
nameserver 10.0.0.9
nameserver 10.0.0.10
insert
nameserver 10.0.0.11
nameserver 10.0.0.12
|
Method 2 Output - line count
Quote:
# Generated by NetworkManager
nameserver 10.0.0.1
nameserver 10.0.0.2
nameserver 10.0.0.3
nameserver 10.0.0.4
nameserver 10.0.0.5
nameserver 10.0.0.6
nameserver 10.0.0.7
insert new line
nameserver 10.0.0.8
nameserver 10.0.0.9
nameserver 10.0.0.10
nameserver 10.0.0.11
nameserver 10.0.0.12
|
|
|
|
|
12-24-2009, 03:11 AM
|
#12
|
|
LQ Newbie
Registered: Mar 2008
Location: NJ
Distribution: Suse
Posts: 18
Rep:
|
It seems this should work for you:
Code:
total_line=$(wc -l /home/myfile |cut -d " " -f1) #get line count
insert_point=$(($total_line-2)) #get insert point
sed "$insert_point a insert_text" /home/myfile #insert using append command (a)to sed.
Notice double quotes for variables, (").
Replace "insert_text" with what you want including $variables
You could paste the following in to the above code to confirm you have set the variables correctly.
Code:
echo total_line= $total_line
echo insert_point=$ insert_point
When it all looks right you need to write the file.
So use one of the following:
Code:
#writes /home/my_new_file and preserves /home/myfile
sed "$insert_point a insert_text" /home/myfile > /home/my_new_file
or use the -i option
Quote:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
|
Code:
#overwrites /home/myfile!! be careful!!
sed -i.bak "$insert_point a insert_text" /home/myfile
|
|
|
1 members found this post helpful.
|
12-24-2009, 04:18 AM
|
#13
|
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by mehorter
It seems this should work for you:
Code:
total_line=$(wc -l /home/myfile |cut -d " " -f1) #get line count
|
get the count without using cut
Code:
wc -l < /home/myfile
|
|
|
|
12-24-2009, 04:37 AM
|
#14
|
|
LQ Newbie
Registered: Mar 2008
Location: NJ
Distribution: Suse
Posts: 18
Rep:
|
Quote:
Originally Posted by ghostdog74
get the count without using cut
Code:
wc -l < /home/myfile
|
Very good!
I can shorten this code further!
Code:
insert_point=$(($(wc -l < /home/myfile)-2)) #get insert point
sed "$insert_point a insert_text" /home/myfile #insert using append command (a)to sed.
Last edited by mehorter; 12-24-2009 at 04:53 AM.
Reason: removed extranaous variable name
|
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 05:05 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|