Add one text line in the head of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add one text line in the head of the file
# 1  
Old 03-01-2010
Bug Add one text line in the head of the file

hello,
how can I add one text line string at the line number one of a file.

thankx,
# 2  
Old 03-01-2010
Try this,

Code:
sed -ri '1/s/(.*)/string\1/' file_name

# 3  
Old 03-01-2010
the command not :work as
Code:
-bash-3.00$ sed -ri '1/s/(.*)/"my text"\1/' myfile
sed: illegal option -- r

# 4  
Old 03-01-2010
Hi friend,I found the problem in your command.

You remove the slash character after 1.

You try this,

Code:
sed -ri '1s/(.*)/text\1/' inputfile

Even if you have problem with r option,you try the following.

Code:
sed -i '1s/\(.*\)/text\1/' inputfile

# 5  
Old 03-01-2010
Sed command

Hi Ahmed waheed sorry , I wrongly typed the command.

Try this,

Code:

sed -ri '1s/(.*)/string\1/' file_name

# 6  
Old 03-02-2010
MySQL

See the following example:

it will add the string into the first line of the text file
Code:
sed  -i  '1{s/^/string\n/}' fine_name

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to display certain line of file only using head or tail in 1 command?

First month learning about the Linux terminal and it has been a challenge yet fun so far. We're learning by using a gameshell. I'm trying to display a certain line ( only allowed 1 command ) from a file only using the head or tail. I'm pretty about this answer: head -23 history.txt | tail -1... (1 Reply)
Discussion started by: forzatekk
1 Replies

2. Shell Programming and Scripting

How to add line numbers (multiples of 5: 0,5,10,15,20) to a text file?

Hi, I need to number the lines in my text file. I know how to do this with standard numbering (1,2,3,4, etc) but I need to count in multiples of 5, beginning 0,5,10,15... example existing file: abcd efg hijklm nopqrs desired output 0 abcd 5 efg 10 hijklm 15 ... (11 Replies)
Discussion started by: livbaddeley
11 Replies

3. Shell Programming and Scripting

how to add text into the last line of text file

I need help with insert text to the last line of text file with echo command I know can do something like echo "i4\n$logtext\n.\nwq" | ex -s $file can insert to first line, but how can i change this code in order to insert to the last line of text file? please help, thank you :( (2 Replies)
Discussion started by: gavin_L
2 Replies

4. Shell Programming and Scripting

sed add after line x new text from file

I've been playing with sed, trying to get it to insert the contents of somefile.txt after line 13 on anotherfile.txt. I tried searching for a line with regex and attempting to insert something on the next line with: find ./anotherfile.txt -type f -exec sed -i -e '/^dog/cat/' {} \; but it... (2 Replies)
Discussion started by: unclecameron
2 Replies

5. UNIX for Dummies Questions & Answers

Display lines 30 to 40 of a text file using head and/or tail commands

Assume that the text file contains over 40 lines. How do you do this?!?!? (5 Replies)
Discussion started by: phunkypants
5 Replies

6. Shell Programming and Scripting

Add ; to every line in text file

Please help to add ; to every line in a text file i Have tired sed 's/$/ ; /g' > /tmp/drop_tables.sql but not working :( Thanks (2 Replies)
Discussion started by: bluebird5m
2 Replies

7. Shell Programming and Scripting

appending the count of line in each file at head of each file

hello everybody, I have some files in directory.each file contain some data. my requirement is add the count of each line of file in head of each file. any advice !!!!!!!! (4 Replies)
Discussion started by: abhigrkist
4 Replies

8. Shell Programming and Scripting

how to add line numbers in text file

Hi all How to add line numbers in text file.. ex abcd cdef result 1. abcd 2. cdef thx in advance (4 Replies)
Discussion started by: suryanarayana
4 Replies

9. Shell Programming and Scripting

Need to add a comment line in a text file

Hi I need to add a comment line at the begining of a text file. The scenario is given below. 1. The number of servers that needs to be updated is around 80 2. The location of the text file in all the servers are the same including the file name. 3. The comment has to be added at the very... (2 Replies)
Discussion started by: orakhan
2 Replies

10. Shell Programming and Scripting

Add text to file at a certain line

I am trying to add a line of text just before the last line in a file. For example, if the last line of a file is "exit 0", I need to add a line of text just before that. Any ideas how I might do that? Thanks (5 Replies)
Discussion started by: TheCrunge
5 Replies
Login or Register to Ask a Question