Replace line X with String Y using sed/awk

Replace line X with String Y using sed/awk

am 10.10.2007 20:39:09 von thesoulcrusher

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!

Re: Replace line X with String Y using sed/awk

am 10.10.2007 21:30:22 von Cyrus Kriticos

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

Re: Replace line X with String Y using sed/awk

am 10.10.2007 21:48:08 von Bill Marcum

On 2007-10-10, 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/'

Re: Replace line X with String Y using sed/awk

am 10.10.2007 22:08:37 von Ed Morton

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.

Re: Replace line X with String Y using sed/awk

am 10.10.2007 22:09:03 von Michael Tosch

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

Re: Replace line X with String Y using sed/awk

am 11.10.2007 03:15:48 von thesoulcrusher

On Oct 10, 3:48 pm, Bill Marcum wrote:
> On 2007-10-10, 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/'

Thanks this worked perfectly

Re: Replace line X with String Y using sed/awk

am 23.10.2007 19:22:29 von djcham

On Oct 10, 6:15 pm, thesoulcrusher wrote:
> On Oct 10, 3:48 pm, Bill Marcum wrote:
>
>
>
> > On 2007-10-10, 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/'
>
> Thanks this worked perfectly

Thanks!!!