LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-04-2008, 08:36 PM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
shell script: insert line in a file


I want to append to line number 45 of a file. Currently I am doing echo "something" >> file which puts "something" at the end of the file. But I would like to echo to, say, line number 33.

Thanks for any help.
 
Old 02-04-2008, 08:38 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by noir911 View Post
I want to append to line number 45 of a file. Currently I am doing echo "something" >> file which puts "something" at the end of the file. But I would like to echo to, say, line number 33.

Thanks for any help.
sounds like homework, soooo....

what you need to do is separate the file into parts, output part 1 to a file, then the line to insert, then part 2 to the file.

head and tail can help, i.e. "head -n 10 myfile" will print out the first 10 lines of "myfile". "tail -n 10 myfile" will print out the last 10 lines of "myfile". "wc -l myfile" will count the number of lines in "myfile".

Hopefully that's enough to get you going.

Last edited by BrianK; 02-04-2008 at 08:40 PM.
 
0 members found this post helpful.
Old 02-04-2008, 09:00 PM   #3
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
You could use awk - something like:

awk '{if(NR == 45) {print "new line" >>"myfile"}}'

(Not tested)
 
Old 02-04-2008, 10:15 PM   #4
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BrianK View Post
sounds like homework, soooo....

what you need to do is separate the file into parts, output part 1 to a file, then the line to insert, then part 2 to the file.

head and tail can help, i.e. "head -n 10 myfile" will print out the first 10 lines of "myfile". "tail -n 10 myfile" will print out the last 10 lines of "myfile". "wc -l myfile" will count the number of lines in "myfile".

Hopefully that's enough to get you going.
Sorry if you misunderstood but this is not homework - I needed to add some lines through a shell script to my squid config file.
 
Old 02-04-2008, 10:15 PM   #5
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by JWPurple View Post
You could use awk - something like:

awk '{if(NR == 45) {print "new line" >>"myfile"}}'

(Not tested)
Thanks!This helps. Good ol' awk.
 
Old 02-04-2008, 10:29 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
another way
Code:
# awk 'NR==45{print "newline"}1' file > outfile
 
Old 02-04-2008, 10:42 PM   #7
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
sed way:
Code:
sed '45i\newline' file
use -i to edit in place redirect using '> outfile' to copy it to another file.
Also the backslash isn't necessary, I just put it there for clarity.
Code:
sed '45inewline' file
is the same thing.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 07:32 AM
Script to insert string in first line of a file minil Programming 13 01-02-2006 11:56 PM
command or shell script to print line range from file minil Programming 3 12-28-2005 08:05 AM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM


All times are GMT -5. The time now is 02:24 PM.

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