LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 12-23-2009, 11:21 AM   #1
xonar
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 0
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.
 
Old 12-23-2009, 11:33 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by xonar View Post
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.
 
Old 12-23-2009, 12:09 PM   #3
slacker_et
Member
 
Registered: Dec 2009
Distribution: Slackware
Posts: 113

Rep: Reputation: 23
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
 
Old 12-23-2009, 12:14 PM   #4
xonar
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 12-23-2009, 12:16 PM   #5
xonar
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 12-23-2009, 12:28 PM   #6
xonar
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 12-23-2009, 12:59 PM   #7
slacker_et
Member
 
Registered: Dec 2009
Distribution: Slackware
Posts: 113

Rep: Reputation: 23
Quote:
Originally Posted by xonar View Post
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.
Old 12-23-2009, 07:16 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
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.
Old 12-23-2009, 10:09 PM   #9
xonar
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 12-23-2009, 10:25 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
run the script with print statements and see the output for yourself.
 
Old 12-24-2009, 12:08 AM   #11
xonar
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 12-24-2009, 03:11 AM   #12
mehorter
LQ Newbie
 
Registered: Mar 2008
Location: NJ
Distribution: Suse
Posts: 18

Rep: Reputation: 3
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.
Old 12-24-2009, 04:18 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by mehorter View Post
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
 
Old 12-24-2009, 04:37 AM   #14
mehorter
LQ Newbie
 
Registered: Mar 2008
Location: NJ
Distribution: Suse
Posts: 18

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
script to insert no. of lines in a file to the start of the file LarsB Linux - Newbie 3 09-17-2009 08:22 PM
SHELL Script to insert lines after certain Intervals rahulruns Linux - General 9 02-26-2008 01:21 AM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 07:48 AM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 05:30 PM


All times are GMT -5. The time now is 10:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration