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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add one line in the beginning of the file?
# 1  
Old 06-25-2014
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
Code:
 
abc
cde
add
xyz

output file

Code:
 
newline
abc
cde
add
xyz

thanks in advance.
# 2  
Old 06-25-2014
Code:
$ echo "newline" | cat - infile > outfile

Code:
$ cat <(echo "newline") infile >outfile

Code:
$ awk 'BEGIN{print "newline"}1' infile >outfile

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 06-25-2014
Code:
sed '1i newline' infile
newline
abc
cde
add
xyz

# 4  
Old 06-25-2014
Code:
cat - file <<< newline

This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-25-2014
Quote:
Originally Posted by in2nix4life
Code:
sed '1i newline' infile
newline
abc
cde
add
xyz

Thanks for you reply.

when run the command, I got below error.
sed: command garbled: 1i newline

my server is SunOS 5.10 Generic_144488-17 sun4v sparc SUNW,SPARC-Enterprise-T5220
# 6  
Old 06-25-2014
Code:
awk 'BEGIN { print "newline" } ; 1' inputfile > outputfile

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 06-25-2014
If you want to use sed, this should work:
Code:
sed '1i\
newline
' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

3. 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

4. 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

5. Windows & DOS: Issues & Discussions

Trying to add text to the beginning of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (6 Replies)
Discussion started by: pasc
6 Replies

6. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

7. Shell Programming and Scripting

add a number to the beginning of every line

hey, i would like to add a line number to the beginning like so: red blue green yellow will be: 1=>red 2=>blue 3=>green 4=>yellowplease advise thank u. (5 Replies)
Discussion started by: boaz733
5 Replies

8. Linux

Add file's date at beginning of every line in file

How would I do that? /Rutger (6 Replies)
Discussion started by: rutgerblom
6 Replies

9. 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

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