best way to insert a line at the top of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting best way to insert a line at the top of a file?
# 8  
Old 02-09-2009
Quote:
but honestly.... no, there isn't a better way.
No, that is not correct...

Quote:
you can use the sed "insert" comand:
Yes I agree.. use the sed insert command, and you can even do it inline so that you do not have to pipe to another file then replace the old with the new.

Code:
[user@host ~]$ cat lines.txt
LineA
LineB
LineC

[user@host ~]$ sed -i '1 i \Line1' lines.txt
[user@host ~]$ cat lines.txt
Line1
LineA
LineB
LineC

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. Shell Programming and Scripting

Insert date/time header at top of file

I'm trying to take mrt output and put it at the top of a file along with the date and time. I was able to do it at the bottom of the file with the following printf "********** $(date) **********\n\n" >> $OUTPUT_PATH/$HOSTNAME mtr -r -w -c 10 $HOSTADDRESS >> $OUTPUT_PATH/$HOSTNAME printf... (2 Replies)
Discussion started by: kramer65
2 Replies

4. Shell Programming and Scripting

Insert a new line before every 5th line in a file

Hi, I need to insert a new line containing the string "QUERY" above every 5 lines. The below piece of code inserts a new line after every 5th line awk '{print $0} !(NR%5) {print "QUERY"}' sed 'n;n;n;n;G;' --> I do not know how to give "QUERY" string here But I need to insert it before... (4 Replies)
Discussion started by: royalibrahim
4 Replies

5. UNIX for Advanced & Expert Users

Insert string in binary file at top

How can i append a EBCDIC string of 100 bytes to 0th position of a binary file in UNIX. (4 Replies)
Discussion started by: param_it
4 Replies

6. Shell Programming and Scripting

append a line into a file in the top

hi, My code is #!/bin/sh echo "\n\nPlease enter the month of the year(YYYYMM) : \c" read date_rep INPUT_L9_FILE=L9_Recharge_Description_EOM_$date_rep.csv #This part is used to summarise Grand_Total, Balance_Total of file L9_Recharge_Description_EOM_${1}.csv. awk -F"," '{if(NR!=1)... (5 Replies)
Discussion started by: madfox
5 Replies

7. UNIX for Dummies Questions & Answers

how do I insert argument into TOP of file using vi?

when directing some text into a file can you choose where it goes like the top of the file (which is text aswell) or the middle?? if so how - especially would like to know how to do so in vi (text editor) If i were to enter an argument ($1) into a another argument ($2) would it would be... (6 Replies)
Discussion started by: rprules
6 Replies

8. Shell Programming and Scripting

insert text into top of file

how would you insert text into a existing file using aguments first arguments being the line of text and the second argument being file name (1 Reply)
Discussion started by: jimbob
1 Replies

9. UNIX for Advanced & Expert Users

Insert a line as the first line into a very huge file

Hello, I need to insert a line (like a header) as the first line of a very huge file (about 3 ml rows). I am able to do it with sed, but redirecting the output and creating a new file takes quite some time. I was wondering if there was a more efficient way of doing it? Any help would be... (3 Replies)
Discussion started by: shriek
3 Replies

10. Shell Programming and Scripting

SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed? (12 Replies)
Discussion started by: MBGPS
12 Replies
Login or Register to Ask a Question
tmpfile(3)						     Library Functions Manual							tmpfile(3)

NAME
tmpfile - Creates a temporary file LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <stdio.h> FILE *tmpfile ( void ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: tmpfile(): XSH4.2 Refer to the standards(5) reference page for more information about industry standards and associated tags. DESCRIPTION
The tmpfile() function creates a temporary file and returns its FILE pointer. The file is opened for update. The temporary file is automat- ically deleted when the process using it terminates. RETURN VALUES
Upon successful completion, the tmpfile() function returns a pointer to the stream of the file that is created. Otherwise, it returns a null pointer and sets errno to indicate the error. ERRORS
The tmpfile() function sets errno to the specified values for the following conditions: A signal was caught during tmpfile(). OPEN_MAX file descriptors are currently open in the calling process. [Tru64 UNIX] All available file descriptors are currently open in the calling process. The maximum allowable number of files is currently open in the system. The directory or file system that would contain the new file cannot be expanded. RELATED INFORMATION
Functions: fopen(3), mktemp(3), tmpnam(3), unlink(2), getdtablesize(2) Standards: standards(5) delim off tmpfile(3)