The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
see a specific line in file zivsegal UNIX for Dummies Questions & Answers 7 04-23-2009 07:08 PM
how do I filter for a specific line in a file? gelitini UNIX for Dummies Questions & Answers 5 06-26-2008 04:26 PM
grep on specific line of the file rider29 Shell Programming and Scripting 4 05-29-2008 09:57 AM
How to change a specific character in a file sdubey Shell Programming and Scripting 6 02-22-2008 03:30 PM
Add a line to a specific spot in a file d__browne UNIX for Advanced & Expert Users 6 12-07-2006 07:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-18-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
Change Specific Line of a File

Hi everyone,

I am attempting to do something that should be very simple. How do I replace a specific line of a file with different text, and then save that file to its original name? I believe I want to use the sed command with the c option, but I after trying many times, I can't get the right syntax. I am using bash.

Mike
  #2 (permalink)  
Old 08-18-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Code:
different_text="this is the new text"
awk -v diff="$different_text" '{ if(NR==4) { print diff}
                              else {print $0} } ' filename > newfilename
  #3 (permalink)  
Old 08-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Code:
sed '42c\'"I am the walrus" file >newfile
  #4 (permalink)  
Old 08-18-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
Hi,

Yes I was trying to something similar. However, that still does not work. Is that the syntax for bash?
  #5 (permalink)  
Old 08-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Works here, Ubuntu 7.something, bash 3.2.13. The sed version is what decides whether it works or not, though. I have GNU sed version 4.1.5

Code:
vnix$ sed '4c\'"I am the walrus" /etc/motd
Linux left 2.6.20-17-386 #2 Thu Jul 10 00:02:05 UTC 2008 i686

The programs included with the Ubuntu system are free software;
I am the walrus
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Try putting the change on a different line:

Code:
sed '42c\
I am the walrus' file >newfile
Yes, you would put a backslash and a newline between "42c" and "I am the walrus". Yes, that's right. No, I'm not kidding. (Nothing personal, it's just that the normal reaction to this instruction is "I tried it and it doesn't work." "So did you put a backslash and a newline between the two lines?" "No, was I supposed to?")

Last edited by era; 08-18-2008 at 06:10 PM..
  #6 (permalink)  
Old 08-18-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
Perhaps i am not going about this correctly. Here is my "test file":

$cat test.txt
coo
coo
ca
choo
$

Let's say I simply want to replace the first line (coo) with koo.

With the above suggestion:

$sed '1c\'"koo" test.txt
sed: 1: "1c\koo": extra characters after \ at the end of c command

Putting a space before "koo" yields:

$sed '1c\' "koo" test.txt
sed: koo: No such file or directory
coo
ca
choo

I get closer by typing:

$sed '1c\
> koo' test.txt
koocoo
ca
choo

But it's still not right. I am using the bash that comes with OS X Leopard. Any ideas on what's going wrong?
  #7 (permalink)  
Old 08-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Probably your version of sed is different; OSX is traditionally BSD based.

Try this:

Code:
sed '1c\
koo\
' test.txt
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 02:16 PM.


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