Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to replace date string on line 'n' with current date? 1

Status
Not open for further replies.

Kipnep70

Technical User
Nov 18, 2003
81
US
I have a script that I have to edit and change a date on a specific line in the following format:(mm/dd/yyyy)

Its on line 38, and its the 9th field in the record. (Assuming that each line is a different record)

There are other dates in the file but I only want that specific what changed. I'm kinda new to awk and unix (I'm still learning quite a few commands in unix) ... anyway is there a way I can somebody help me use awk to replace the old date with the current date?

Would sed be more appropriate for this? (I'd have to learn sed in that case..)

I think sed would be something like: sed 's/??\/??\/06/`date +%m\/$d\/%Y'


any help would be great, tia

Kip
 
sed would be fine. You need to put an address at the beginning of the command though to make it only take effect on line 9. Also you can't use `date ...` inside single quotes as they prevent it from being evaluated.

Also you haven't terminated your backquotes around the date command.

Also you need to use "." instead of "?". ? means "0 or more occurrences of the preceding expression". "." just matches a single character.

Where does $d come from? Did you mean %d?

I think something like this will do what you want:

Code:
sed '9s/..\/..\/06/'`date +%m/$d/%Y`'/'[code]


Annihilannic.
 
Hi

Kip said:
Its on line 38, and its the 9th field in the record.
You did not specified if the fieds are separated or fixed length. Supposing that are separated with \t
( tab ) characters :
Code:
sed '[red]38[/red]s,[^[green]\t[/green]]\+,NEWDATE,[blue]9[/blue]' /input/file
[gray]#[/gray]    [red]^^[/red]    [green]^^[/green]            [blue]^[/blue]
[gray]#[/gray]    [red]|[/red]     [green]|[/green]             [blue]+- field number[/blue]
[gray]#[/gray]    [red]|[/red]     [green]+- field separator[/green]
[gray]#[/gray]    [red]+- line number[/red]

[gray]# or[/gray]

awk -F '[green]\t[/green]' 'NR==[red]38[/red]{$[blue]9[/blue]="NEWDATE"}1'
[gray]#[/gray]       [green]^^[/green]       [red]^^[/red]  [blue]^[/blue]
[gray]#[/gray]       [green]|[/green]        [red]|[/red]   [blue]+- field number[/blue]
[gray]#[/gray]       [green]|[/green]        [red]+- line number[/red]
[gray]#[/gray]       [green]+- field separator[/green]
Note that the above codes are examples, not solutions. You have to adapt them.

Feherke.
 
Hmmm... I didn't read those field and record numbers right at all, did I? :)

Annihilannic.
 
This is what I got so far...

Code:
awk -F ' ' 'NR==38{$9=system("date +%m/%d/%Y")}1' < altsite-rpt.jwb

but it's returning a '0' instead of the actually date. I assume I'm getting a 0 because the command is running successfully.
 
Hi

Kipnep70 said:
it's returning a '0' instead of the actually date. I assume I'm getting a 0 because the command is running successfully.
Right.
Code:
awk -F ' ' 'NR==38{"date +%m/%d/%Y"|getline $9}1' altsite-rpt.jwb

[gray]# or[/gray]

awk -F ' ' -v nd=`date +%m/%d/%Y` 'NR==38{$9=nd}1' altsite-rpt.jwb
Advice : let [tt]awk[/tt] read the input file itself, so leave out the less-then ( < ) sign.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top