LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   insert lines into a file after calculating where to insert (http://www.linuxquestions.org/questions/programming-9/insert-lines-into-a-file-after-calculating-where-to-insert-777704/)

xonar 12-23-2009 12:21 PM

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.

Sergei Steshenko 12-23-2009 12:33 PM

Quote:

Originally Posted by xonar (Post 3802624)
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.

slacker_et 12-23-2009 01:09 PM

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

xonar 12-23-2009 01:14 PM

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.

xonar 12-23-2009 01:16 PM

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.

xonar 12-23-2009 01:28 PM

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

slacker_et 12-23-2009 01:59 PM

Quote:

Originally Posted by xonar (Post 3802687)
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

ghostdog74 12-23-2009 08:16 PM

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


xonar 12-23-2009 11:09 PM

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?

ghostdog74 12-23-2009 11:25 PM

run the script with print statements and see the output for yourself.

xonar 12-24-2009 01:08 AM

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

mehorter 12-24-2009 04:11 AM

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


ghostdog74 12-24-2009 05:18 AM

Quote:

Originally Posted by mehorter (Post 3803301)
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

mehorter 12-24-2009 05:37 AM

Quote:

Originally Posted by ghostdog74 (Post 3803361)
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.



All times are GMT -5. The time now is 03:49 PM.