The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Question regarding lm file mr_deb UNIX for Dummies Questions & Answers 5 05-22-2008 08:22 AM
mk file question felixmat1 Shell Programming and Scripting 2 10-11-2007 11:51 PM
Post Shell programming: Question about source a file and read data from the file ccwq Shell Programming and Scripting 3 08-04-2007 11:28 PM
file locking question Bab00shka UNIX for Dummies Questions & Answers 2 01-29-2004 10:34 AM
Newbie question about difference between executable file and ordinary file Balaji UNIX for Dummies Questions & Answers 1 11-29-2000 06:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-12-2002
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
file question

without using a file stream editor such as sed, is there a way to echo the value of a variable to a specific location in a file (i.e. if the file has 20 lines, can I add a new line with the contents of the variable FOO at line 1?) ? I should add that the file will be created during run time. The shell will create the file, capture a output from a spool command, then slap a header record and trailer record on it. The header cannot be initially created because its dependant upon the output of the spool.
  #2 (permalink)  
Old 08-12-2002
TioTony's Avatar
TioTony TioTony is offline Forum Advisor  
Bit Pusher
  
 

Join Date: Oct 2001
Location: Southern California
Posts: 332
You could do something like this:

echo "HEADER" > tmpfile.txt
cat logfile.txt >> tmpfile.txt
mv tmpfile.txt logfile.txt
  #3 (permalink)  
Old 08-12-2002
peter.herlihy peter.herlihy is offline
Registered User
  
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
Why not use sed?
  #4 (permalink)  
Old 08-13-2002
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
I was more curious than anything as to whether there is a way to insert a line of data into a file (at any given line number) without using an editor such as sed.

What I really would like to know is if I can accomplish this without using intermediate files such as noted by the first reply.

How can I do this with sed?
  #5 (permalink)  
Old 08-13-2002
peter.herlihy peter.herlihy is offline
Registered User
  
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
Unfortunately sed requires the use of temporary files as you can't output to the original file, so either way you're going to have to use temp files.

See this thread for the sed syntax on inserting. Note in the examples 1 is used for the insert, but you can specify the line number as different.
  #6 (permalink)  
Old 08-21-2002
mpolyakov mpolyakov is offline
Registered User
  
 

Join Date: Aug 2002
Location: Chicago
Posts: 5
If you depend on the line numbers for the insertion,
probably the easiest way is to use awk (nawk).

awk '
# Insert line in the file before line 5
NR=5 { print"The line you need"}
{print}
' infile > outfile
  #7 (permalink)  
Old 08-21-2002
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
What about using the shell?

This will take whatever is in "file1", and place a seperator line before line 5:

Code:
#! /bin/ksh

integer n=1
while read line; do
 (( n == 5 )) && { print "#------- Seperator ------#"; }
 print $line
 n=n+1
done < file1

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0