The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to Append a Value to each line of the file dsshishya UNIX for Dummies Questions & Answers 11 03-21-2008 02:48 PM
How to append words at the end of first line lmatlebyane Shell Programming and Scripting 11 02-28-2008 04:39 AM
append a line to the last line in a file subhrap.das UNIX Desktop for Dummies Questions & Answers 5 04-25-2007 10:17 AM
using sed to append text to the end of each line Redg UNIX for Dummies Questions & Answers 8 07-26-2006 11:59 AM
Using SED to append character to each line c0nn0r Shell Programming and Scripting 4 07-08-2006 10:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-06-2006
ymeyaw ymeyaw is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 3
append line

dear all,
i am fresh for unix, and currently i wish to insert a line into a txt file as below:

test.txt
------
abc
def
ghi

and i wish to insert "hello world" in between abc and def line:

output wanted:
------------------
abc
hello world
def
ghi

what should be the "sed" use?
  #2 (permalink)  
Old 04-06-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Here's an awkward awk bit:

Code:
$ cat testfile
abc
def
ghi
$ awk 'NR==2 {printf "hello world\n"} {print $0}' testfile
abc
hello world
def
ghi


Last edited by blowtorch; 04-06-2006 at 10:33 AM.. Reason: changed code
  #3 (permalink)  
Old 04-06-2006
ymeyaw ymeyaw is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 3
thanks alot ..it 's working!!
  #4 (permalink)  
Old 04-06-2006
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
If you want to use sed

Code:
# sed '/abc/ a\
hello world!' test.txt

Cheers
ZB
  #5 (permalink)  
Old 04-09-2006
beginner1 beginner1 is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 13
How do you make the changes permanent in the file. I want to be able to save the changes from the awk command.

i tried the example above, then issued the cat command but the "hello world" wasnt saved to the file.

$ cat testfile
abc
def
ghi


Also when I try the command with a file called file11 with the following data:
Line1
Line2
Line3

I get the following output:
Line1hello world
Line2Line3
  #6 (permalink)  
Old 04-09-2006
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,794

Code:
awk 'NR==2 {printf "hello world\n"} {print $0}' testfile > newfile

newfile now has hello world in it
  #7 (permalink)  
Old 04-09-2006
beginner1 beginner1 is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 13
Great thanks. That solved the problem of how to save it.
The other problem that I have is that the output seems to join together whereas I want it inserted in a new line.

e.g. file1 with the following data
Line1
Line2
Line3


When I check the results in the file it saves to, the output comes out like this:

Line1Hello world
Line2Line3


Whereas I would like the output to be as follows:

Line1
Hello world
Line2
Line3
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0