LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-30-2004, 04:47 PM   #1
false-hopes
LQ Newbie
 
Registered: Mar 2004
Location: I don't remeber, something about an asylum, something about insane too.
Distribution: Gentoo, Knoppix, Debian
Posts: 20

Rep: Reputation: 0
Inserting lines into a file through shell scripting


I'm not sure if this is the right place to ask, but I don't know where else is either. Does anyone know any way I can through shell scripting, insert lines into a file at a specified point, either after a certain number of bytes/kilobytes or a certain number of lines, the lines would be the best.
 
Old 10-22-2005, 11:39 AM   #2
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
Making head or tail of it

My apologies for the pun ...

Here's a code fragment that may help:
Code:
# define source file
OLD=<source file>

# define insert file
TXT=<insert file>

# define result file
NEW=<result file>

# set insert point
N=<insert point>


# calculate file length
L=`wc -l $OLD  | awk '{print $1}'`

# calculate remaining file length
(( M = $L - $N + 1))

# assemble the new file
head -$N $OLD  > $NEW
cat      $TXT >> $NEW
tail -$M $OLD >> $NEW
There are other ways to get/define the inserted text, amongst them:
  • "here" doc embedded in the script
  • reading the text from the command line
  • reading a file name from the command line
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting: how to load a function from a file? frankie_DJ Programming 2 08-16-2005 02:16 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 06:18 PM
[c shell] How do I find how many lines a file has? saiz66 Programming 5 10-08-2004 03:01 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM
Shell scripting from java file karthiknataraj Linux - General 1 02-08-2003 04:47 AM


All times are GMT -5. The time now is 02: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