add a new line on top of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers add a new line on top of a file
# 1  
Old 09-01-2010
add a new line on top of a file

infile
Code:
a b c
A E F
1 2 3

outfile
Code:
new line
a b c
A E F
1 2 3

I tried:

Code:
sed '1i\
new line' infile > outfile

cat outfile
Code:
new linea b c
A E F
1 2 3

I don't want the new line be added to the existing first line.
Thanks
Joseph
# 2  
Old 09-01-2010
Code:
awk 'BEGIN{print "new line"}{print}' infile > outfile

or a non-elegant but effective method.
Code:
echo "new line" >foo
cat foo infile >outfile


Last edited by frank_rizzo; 09-01-2010 at 10:05 PM.. Reason: correct spelling
# 3  
Old 09-01-2010
Quote:
Originally Posted by frank_rizzo
Code:
awk 'BEGIN{print "new line"}{print}' infile > outfile

or a non-elegant but effective method.
Code:
echo "new line" >foo
cat foo infile >outfile

awk works fine
Code:
$awk 'BEGIN{print "new line"}{print}' infile > outfile
$cat outfile
new line
a b c
A E F
1 2 3

but the other dosen't look right

Code:
$ echo "new line" >foo
$ cat foo infile >outfile
$ cat outfile
new line
a b c
A E F
1 2 3 $

# 4  
Old 09-01-2010
Quote:
Originally Posted by jdhahbi
but the other dosen't look right
looks like your missing a new line on the last line in infile.
# 5  
Old 09-02-2010
got it. thanks

---------- Post updated 09-02-10 at 05:27 PM ---------- Previous update was 09-01-10 at 06:38 PM ----------

Hi frank_rizzo

I have a follow up question.
my new line must include ' ' or " " as follows:
Code:
name=test color='255,0,0 0,0,255'

or
Code:
name=test color="255,0,0 0,0,255"

infile:
Code:
a b c
A E F
1 2 3

outfile should look like this
Code:
name=test color='255 0 00 0 255'
a b c
A E F
1 2 3


or like this:
Code:
name=test color="255,0,0 0,0,255"
a b c
A E F
1 2 3

here is what I got when I used ' '

Code:
awk 'BEGIN{print "name=test color='255,0,0 0,0,255'"}{print}' infile 
awk: non-terminated string name=test ... at source line 1
 context is
	 >>>  <<< 
awk: giving up
 source line number 2

when I use " "

Code:
awk 'BEGIN{print "name=test color="255,0,0 0,0,255""}{print}' infile 
name=test color=255 0 00 0 255
a b c
A E F
1 2 3

thanks
# 6  
Old 09-02-2010
Try...
Code:
awk 'BEGIN{print "name=test color=\"255,0,0 0,0,255\""}{print}' infile

This User Gave Thanks to Ygor For This Post:
# 7  
Old 09-02-2010
Quote:
Originally Posted by Ygor
Try...
Code:
awk 'BEGIN{print "name=test color=\"255,0,0 0,0,255\""}{print}' infile

what about if add another term to my new line;
my new line looks like tihs:
Code:
name=test color="255,0,0 0,0,255" priority=10

Code:
awk 'BEGIN{print "name=test color=\"255,0,0 0,0,255\"" priority=10}{print}' infile
awk: syntax error at source line 1
 context is
    BEGIN{print "name=test color=\"255,0,0 0,0,255\"" >>>  priority= <<< 10}{print}
awk: illegal statement at source line 1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move a line to top of the file

Hi, I have a following file and it has only one occurrence of line that says "Output view:". It could be in middle somewhere ( i don't know the exact location ). I want to move it as the first line of the file. Input AAA BBBB CCCC Output view: XXXX YYYY ZZZZ Output should be: Output... (13 Replies)
Discussion started by: jakSun8
13 Replies

2. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

3. Shell Programming and Scripting

Grabbing top line of text in a file and setting it to a variable

If I have a txt file with test.txt somelineoftext and I want to set that line of text to variable in a script: so #!/bin/bash var='' becomes #!/bin/bash var='somelineoftext' (3 Replies)
Discussion started by: digitalviking
3 Replies

4. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

5. Shell Programming and Scripting

howto add line as a first line into a non empty file

Hi Trying to do like this : echo "$variable1\n $(cat file.txt)" but it only adds one time. When I run this cmd again with different variable it only replaces line of variable1. How to add constantly line into first line in file ? (3 Replies)
Discussion started by: presul
3 Replies

6. Shell Programming and Scripting

Add new parameters into a line, and redirect the line to other file

How can i add new parameters into a line, and redirect the line to other file? For example: 1.sh name:owner google:richard youtube:student I want a, for example 2.sh with: name:owner:description google:richard:search site youtube:student:video site In the 2.sh, I added a new column:... (7 Replies)
Discussion started by: rafazz
7 Replies

7. Shell Programming and Scripting

Get the bottom line of a file to the top of the file

Hi, i have a small requirement where i have to get the bottom most line from a file to the topmost position. a small example is shown below.. $ cat beep.txt It is first documented as being played in southern England. In the 16th century. By the end of the 18th century, Cricket is a... (5 Replies)
Discussion started by: Shellslave
5 Replies

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

9. Shell Programming and Scripting

best way to insert a line at the top of a file?

say I want to insert "this is a test" as the first line into file A, besides echo "this is a test" > /tmp/tmpfile cat /tmp/tmpfile fileA >> /tmp/result, is there any simple way I can do it? thanks (7 Replies)
Discussion started by: fedora
7 Replies

10. AIX

want to remove some line from top of file.

Hi All, I have AIX 5.3 server. I have one big file. in that i want to remove 5000 line from top. is there any command for this? Thanks, Vishal (6 Replies)
Discussion started by: vishalpatel03
6 Replies
Login or Register to Ask a Question