Change Specific Line of a File | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

Change Specific Line of a File

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-18-2008
msb65 msb65 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 12 December 2011, 2:18 PM EST
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 08-18-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 December 2014, 6:58 PM EST
Location: NM
Posts: 10,313
Thanks: 297
Thanked 823 Times in 765 Posts

Code:
different_text="this is the new text"
awk -v diff="$different_text" '{ if(NR==4) { print diff}
                              else {print $0} } ' filename > newfilename

Sponsored Links
    #3  
Old 08-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts

Code:
sed '42c\'"I am the walrus" file >newfile

    #4  
Old 08-18-2008
msb65 msb65 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 12 December 2011, 2:18 PM EST
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

Yes I was trying to something similar. However, that still does not work. Is that the syntax for bash?
Sponsored Links
    #5  
Old 08-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
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 07:10 PM..
Sponsored Links
    #6  
Old 08-18-2008
msb65 msb65 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 12 December 2011, 2:18 PM EST
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #7  
Old 08-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
Probably your version of sed is different; OSX is traditionally BSD based.

Try this:


Code:
sed '1c\
koo\
' test.txt

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 change a number on a specific line with cshell or shell? miriammiriam Shell Programming and Scripting 9 07-23-2012 09:52 AM
How to change a number on a specific lines in a file with shell? miriammiriam Shell Programming and Scripting 11 03-21-2012 12:09 PM
Change specific ip address in a file iga3725 Shell Programming and Scripting 9 04-23-2010 05:48 PM
Script to change/find/delete/install a specific file Great Uncle Kip UNIX for Dummies Questions & Answers 6 01-07-2009 08:14 AM
How to change a specific character in a file sdubey Shell Programming and Scripting 6 02-22-2008 03:30 PM



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