write new line at the beginning of an existing file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers write new line at the beginning of an existing file
# 1  
Old 11-06-2008
write new line at the beginning of an existing file

I was trying to find out the easiest way to write new line to the beginning of an exisiting file.
I am using KSH.
# 2  
Old 11-06-2008
Code:
echo "my first new line" > tmp
cat oldfile >> tmp
mv tmp oldfile

one way.
# 3  
Old 11-06-2008
The easiest way is IMHO:

Code:
vi file
O
<ESCAPE>
ZZ

# 4  
Old 11-06-2008
Thank you for the replies.

I was able to write to the first line using the following command:
echo "1i\nthis is the line that u wanted to insert \n.\nwq" | ex -s filename


But could not understand the meaning though..
Could someone explain.

Thanks In Advance.
# 5  
Old 11-06-2008
Code:
echo "1i\nthis is the line that u wanted to insert \n.\nwq" | ex -s filename

echo "string...." ex -s filename means to invoke the ed (or vi) editor thru the ex program. -s means "silent" do not give responses.

1i\n = goto line 1 go into insert mode add a new line then insert the text
\n.\nwq - add a newline at the end of the text, then write the changes to the file and quit.

Most people don't necessarily view this as easier than some other more "scripty" ways.
# 6  
Old 11-06-2008
Thank you for the detailed explanation.Now I understand what it is..
I did not want to use tmp files hence used this approach..

Thanks again
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write data into existing '.xlsx' file that has multiple sheets

Hello, I have a .xlsx file with 11 worksheets and I need to insert the contents of a text file (tab delim, roughly 30 columns with 1000 rows) from Row 3 onwards on the 2nd sheet. I have my code which can insert values into existing sheet but I am unable to write/import the contents of a text... (1 Reply)
Discussion started by: nans
1 Replies

2. Shell Programming and Scripting

Write data into existing '.xlsx' file that has multiple sheets

Hello, I have a .xlsx file with 11 worksheets and I need to insert the contents of a text file (tab delim, roughly 30 columns with 1000 rows) from Row 3 onwards on the 2nd sheet. I have my code which can insert values into existing sheet but I am unable to write/import the contents of a text... (0 Replies)
Discussion started by: nans
0 Replies

3. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

4. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

5. UNIX for Dummies Questions & Answers

Add word/value at the beginning of each line in a file

how to add value/word at the beginning of each line in a file ? i have file number.txt and the output is below 1000 1001 1002 1003 1004 i want to add 000 at the beginning of each line, desire output is below 0001000 0001001 0001002 0001003 0001004 and so on please advise how... (5 Replies)
Discussion started by: jason6247
5 Replies

6. Shell Programming and Scripting

Append file name to the beginning of each line

I want to append file names at the beginning of a line for each row file content abc.txt.gz 123|654|987 bcd.txt.gz 876|trf|kjh I want a single output file with below format abc.txt.gz|123|654|987 bcd.txt.gz|876|trf|kjh This one is working but only with unzip files,need to have... (3 Replies)
Discussion started by: rakesh5300
3 Replies

7. Shell Programming and Scripting

How to write a new entry at the beginning of a log file instead of at the end?

Hi Ladies and Gents, Explanation of my question with an example: Let's consider the script: backup_every_hour.sh #!/bin/bash rsync -auv $dir $backup_dir >> backup_every_hour_script.log Each time this script is called there will be a new entry at the end of the file... (1 Reply)
Discussion started by: freddie50
1 Replies

8. UNIX for Dummies Questions & Answers

Adding one string at the beginning of each line in a file

Hi, I have file a.txt as below. I want to add one string root beginning of each line. Sample file a.txt aaa bbb ccc Sample output Root aaa Root bbb Root ccc Can any one help me on this? (6 Replies)
Discussion started by: siba.s.nayak
6 Replies

9. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

10. Shell Programming and Scripting

input a line at the beginning of every file in a directory?

if need to input a word or anything at the beginning of every file in a directory. how do i accomplish this? say the file is named hyperten. how do i make hyperten the first line of every file in a given directory? thanks (6 Replies)
Discussion started by: Terrible
6 Replies
Login or Register to Ask a Question