Inserting argument into top of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting argument into top of a file
# 1  
Old 10-05-2006
Question Inserting argument into top of a file

Hi,

I am new to Unix, and I am trying to append a line of argument into a current file. I need this line to be inserted into the very top of the file. Does anyone know how this is done?

For example, I am trying: echo "insert to top" >> filename. This inserts the line at the bottom of the file. How do I get it to insert to the top??

Please help!
# 2  
Old 10-05-2006
Code:
{
  echo "This is the first line."
  cat originalFile
} > newFile
mv newFile originalFile

# 3  
Old 10-05-2006
Hi Glenn,

Thank you for your input. I cannot do the way you are showing me. My task is to input a line to an existing file without creating new or temporary file.

That is why I have used the >> symbol, but this inserts the line at the bottom. Do you know how to insert to the top without creating extra files?
# 4  
Old 10-05-2006
Code:
/bin/echo '1i\nNew Line Goes Here\n.\nwq' | ex -s myFile

# 5  
Old 10-05-2006
Whenever I see "without creating new or temporary file", I think "homework". You can use perl to edit files in place. Search the forums -- you will find many examples of this.
# 6  
Old 10-05-2006
Thanks for the input vgersh99. But again, that line of code inserts the line at the end of the file.. I am trying to get the line to be inserted at the top.

And Glenn, I understand where you are coming from, but I just don't wont to have so many temporary files created for no reason.

Any ideas??
# 7  
Old 10-05-2006
Code:
printf '1i\nNew Line Goes Here\n.\nwq!\n' | ex myFile

OR [in-line]

Code:
ex myFile <<EOF
1i
New Line Goes Here
.
wq
EOF

either one of the above inserts a line as the TOP line in a file.

Last edited by vgersh99; 10-05-2006 at 02:29 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting IDs from a text file into a sequence alignment file

Hi, I have one file with one column and several hundred entries File1: NA1 NA2 NA3And now I need to run a command within a mapping aligner tool to insert these sample names into a sequence alignment file (SAM) such that they look like this @RG ID:Library1 SM:NA1 PL:Illumina ... (7 Replies)
Discussion started by: nans
7 Replies

2. Shell Programming and Scripting

Inserting filename at the top of file

Hi Forum members, I want to insert the filename at the top of file (i.e, first row only) My file name is like asdf_432 And I want to insert this filename in first row as filename is asdf with serial no. 432 Thanks in advance. :) (4 Replies)
Discussion started by: CAch
4 Replies

3. HP-UX

Inserting Stinr in between some where in file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me... (0 Replies)
Discussion started by: krsnadasa
0 Replies

4. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

Inserting IP in one line of a file

Hi, I have this line: ip=111.222.133.144,mac=00:16:3E:2A:08:3C,vifname=veth360','ip=10.2.3.4,vifname=veth360a' ^ | ------- I want to insert this IP 144.133.222.111 between "144"... (4 Replies)
Discussion started by: iga3725
4 Replies

7. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

8. Programming

C++ inserting data in a file

Could anyone help me with an efficient(and easy) way to insert data in a file directly(with out using temp file). example open the file1.txt 11112222 333333 44444444 and insert something say " 99999 " somewhere inside the file as 11112222 333 99999 333 44444444 (2 Replies)
Discussion started by: johnbach
2 Replies

9. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

10. 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
Login or Register to Ask a Question