dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

Replace line X with String Y using sed/awk - shell

This is a discussion on Replace line X with String Y using sed/awk - shell ; Hi I have about 30 files, that are just a list of options to run a unix program. I need to find a way to replace a certain line number with a different string I specify. The data in the ...


Home > Database Forum > Operating Systems > shell > Replace line X with String Y using sed/awk

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 10-10-2007, 02:39 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Replace line X with String Y using sed/awk

Hi

I have about 30 files, that are just a list of options to run a unix
program.

I need to find a way to replace a certain line number with a different
string I specify. The data in the files I am looking to replace are
dates.

Here is an example:

file1.txt:

Line 1 : Hello
Line 2 : Goodbye
Line 3 : 10102007
Line 4 : 10312007

For example, say line 3 I want to replace that date of 10102007 with
11012007.

But I do not want to have to reference the value of the file, for
example doing a : sed 's/10102007/11012007/' ... Rather specify the
whole line to replace.

How can I do that?


Thanks!

Reply With Quote
  #2  
Old 10-10-2007, 03:30 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Replace line X with String Y using sed/awk

thesoulcrusher wrote:
>
> I need to find a way to replace a certain line number with a different
> string I specify. The data in the files I am looking to replace are
> dates.
>
> Here is an example:
>
> file1.txt:
>
> Line 1 : Hello
> Line 2 : Goodbye
> Line 3 : 10102007
> Line 4 : 10312007
>
> For example, say line 3 I want to replace that date of 10102007 with
> 11012007.


sed "3s/10102007/11012007/" file1.txt

> But I do not want to have to reference the value of the file, for
> example doing a : sed 's/10102007/11012007/' ... Rather specify the
> whole line to replace.


--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Reply With Quote
  #3  
Old 10-10-2007, 04:08 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Replace line X with String Y using sed/awk

thesoulcrusher wrote:
> Hi
>
> I have about 30 files, that are just a list of options to run a unix
> program.
>
> I need to find a way to replace a certain line number with a different
> string I specify. The data in the files I am looking to replace are
> dates.
>
> Here is an example:
>
> file1.txt:
>
> Line 1 : Hello
> Line 2 : Goodbye
> Line 3 : 10102007
> Line 4 : 10312007
>
> For example, say line 3 I want to replace that date of 10102007 with
> 11012007.
>
> But I do not want to have to reference the value of the file, for
> example doing a : sed 's/10102007/11012007/' ... Rather specify the
> whole line to replace.
>
> How can I do that?
>
>
> Thanks!
>


awk 'NR==3{$0=11012007}1' file

Ed.
Reply With Quote
  #4  
Old 10-10-2007, 04:09 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Replace line X with String Y using sed/awk

thesoulcrusher wrote:
> Hi
>
> I have about 30 files, that are just a list of options to run a unix
> program.
>
> I need to find a way to replace a certain line number with a different
> string I specify. The data in the files I am looking to replace are
> dates.
>
> Here is an example:
>
> file1.txt:
>
> Line 1 : Hello
> Line 2 : Goodbye
> Line 3 : 10102007
> Line 4 : 10312007
>
> For example, say line 3 I want to replace that date of 10102007 with
> 11012007.
>
> But I do not want to have to reference the value of the file, for
> example doing a : sed 's/10102007/11012007/' ... Rather specify the
> whole line to replace.
>
> How can I do that?
>
>



sed '3s/ : .*/ : 11012007/'

awk -v v=11012007 'BEGIN{FS=OFS=" : "} NR==3{$2=v} 1'

--
Michael Tosch @ hp : com
Reply With Quote
Reply

Thread Tools
Display Modes


LinkBacks (?)

LinkBack to this Thread: http://dbaspot.com/forums/shell/270141-replace-line-x-string-y-using-sed-awk.html

Posted By For Type Date
Replace line X with String Y using sed : Linux T&T This thread Pingback 11-11-2008 04:37 AM


All times are GMT -4. The time now is 05:44 PM.