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
explain_tmpfile_or_die(3)				     Library Functions Manual					 explain_tmpfile_or_die(3)

NAME
explain_tmpfile_or_die - create a temporary file and report errors SYNOPSIS
#include <libexplain/tmpfile.h> FILE *explain_tmpfile_or_die(void); FILE *explain_tmpfile_on_error(void); DESCRIPTION
The explain_tmpfile_or_die function is used to call the tmpfile(3) system call. On failure an explanation will be printed to stderr, obtained from the explain_tmpfile(3) function, and then the process terminates by calling exit(EXIT_FAILURE). The explain_tmpfile_on_error function is used to call the tmpfile(3) system call. On failure an explanation will be printed to stderr, obtained from the explain_tmpfile(3) function, but still returns to the caller. RETURN VALUE
The explain_tmpfile_or_die function only returns on success, see tmpfile(3) for more information. On failure, prints an explanation and exits, it does not return. The explain_tmpfile_on_error function always returns the value return by the wrapped tmpfile(3) system call. EXAMPLE
The explain_tmpfile_or_die function is intended to be used in a fashion similar to the following example: FILE *result = explain_tmpfile_or_die(); SEE ALSO
tmpfile(3) create a temporary file explain_tmpfile(3) explain tmpfile(3) errors exit(2) terminate the calling process COPYRIGHT
libexplain version 0.52 Copyright (C) 2009 Peter Miller explain_tmpfile_or_die(3)