Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-11-2010
Registered User
 

Join Date: Jul 2009
Posts: 60
Thanks: 8
Thanked 0 Times in 0 Posts
Append using SED/AWK

Hi friends,

I have a file with content SOME TEXT HERE

I want to append the string GREAT to Line 1 of my file such that the file content should be
GREAT
SOME TEXT HERE

I can do a cat for the string great and file >> newfile and then rename newfile name to file

But this does not put GREAT at Line1. It writes in one single line like GREAT SOME TEXT HERE.

Is there any better way to do this? May be using SED/AWK?

Also please show some pointers to SED/AWk tutorials I would like to start learning them.
Sponsored Links
    #2  
Old 03-11-2010
dennis.jacob's Avatar
Registered User
 

Join Date: Feb 2007
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 10 Times in 9 Posts
Try:


Code:
awk 'NR==1{print "GREAT"; }NR ' file

Sponsored Links
    #3  
Old 03-11-2010
Registered User
 

Join Date: Jul 2009
Posts: 60
Thanks: 8
Thanked 0 Times in 0 Posts
Great! Thank you it works and can you please guide me to some good SED/AWK tutorials?
    #4  
Old 03-11-2010
Registered User
 

Join Date: Sep 2008
Location: In the beautiful World...
Posts: 509
Thanks: 9
Thanked 27 Times in 27 Posts
Awk...

Awk - A Tutorial and Introduction - by Bruce Barnett

Sed...

Sed - An Introduction and Tutorial

regards,
Sponsored Links
    #5  
Old 03-11-2010
devtakh devtakh is offline Forum Advisor  
Unix Enthusiatic
 

Join Date: Oct 2007
Location: Bangalore
Posts: 730
Thanks: 0
Thanked 6 Times in 6 Posts
or

Code:
sed '1i GREAT' file


cheers,
Devaraj Takhellambam
Sponsored Links
    #6  
Old 03-15-2010
ungalnanban's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 142
Thanks: 5
Thanked 17 Times in 6 Posts
Thumbs up

Using sed: append one line in header of the file.
See the following example:


Code:
 cat file
SOME TEXT HERE

 sed  -i  '1{s/^/GREAT\n/}' file

 cat file
GREAT
SOME TEXT HERE

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to append using sed asirohi Shell Programming and Scripting 2 08-24-2009 10:39 AM
append a ' to the $1 $2 ppass Shell Programming and Scripting 2 05-26-2008 09:59 AM
Append using awk Raynon Shell Programming and Scripting 4 11-04-2007 05:19 AM
ftp append brdholman UNIX for Dummies Questions & Answers 1 10-31-2007 09:02 AM
append to fle using sed shweta_d Shell Programming and Scripting 2 06-13-2007 02:57 AM



All times are GMT -4. The time now is 12:59 AM.