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!

Hallo, I have a text file that l

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hallo,

I have a text file that looks something like this:

309;253;88554;10765;191
277;248;119721;6729;201

Using an awk script, I insert yesterdays date in front of every line. Yseterdays date is a variable called yesterday.

nawk -v yesterday="$day" -f a.awk file.txt file.txt

###a.awk
{ printf yesterday ; for (i=1; i<=NF; i++) printf(&quot;%s&quot;, $i); print &quot;&quot;}

Is there a way to modify this so that I insert that date after the first semi-colon? So it looks like this:

309;date;253;88554;10765;191
277;date;248;119721;6729;201

Any help much appreciated.

 
###a.awk
{ $1=$1 FS yesterday ; for (i=1; i<=NF; i++) printf(&quot;%s&quot;, $i); print &quot;&quot;} vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
hmm, this seems to put the date at the end of every line...
any idea why?
 
nawk -F ';' -f a.awk vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
hmmm hmmm....

I don't get it. This seems to delete all semi-colons after the first. This is a copy paste, perhaps someone can spot the mistake(s).

Original file:
;49;23;8920;310
;45;21;12951;282
;67;21;5356;284
;50;25;16047;340

Desired result:
;49;06-11-2002;23;8920;310
;45;06-11-2002;21;12951;282
;67;06-11-2002;21;5356;284
;50;06-11-2002;25;16047;340

the date is taken from a variable called $day within the script.

nawk -F ';' -f /path/to/date.awk

###date.awk
{ $1=$1 FS day ; for (i=1; i<=NF; i++) printf(&quot;%s&quot;, $i); print &quot;&quot;}

However I now get this:
;90035124830162
;93031157087203
;100049238920310
;1030452112951282
;110067215356284

The semi-colons have been removed.

Earlier I did this:

nawk -v yesterday=&quot;$day&quot; -f /path/to/date.awk

with teh same date.awk, this added the proper date, but at the end of the lines. Something like this:

;49;23;8920;310;06-11-2002
;45;21;12951;282;06-11-2002
;67;21;5356;284;06-11-2002
;50;25;16047;340;06-11-2002

Where am I goinf wrong?
 
nawk -F ';' -v OFS=';' -f a.awk vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
hmmm hmmm hmmmm
perhaps my version of nawk, awk runs differently?

This is the command line:
nawk -F ';' -v OFS=';' -f date.awk bob1

bob1 being the txt file.
###date.awk
{ $1=$1 FS day ; for (i=1; i<=NF; i++) printf(&quot;%s&quot;, $i); print &quot;&quot;}

Original file (bob1):
;1000;49;23;8920;310
;1030;45;21;12951;282
;1100;67;21;5356;284

Result:
;100049238920310
;1030452112951282
;110067215356284

I am rapidly losing my remaining hair.
 
sorry for the confusion - substitute the assignment to 'day' to anything you want:

nawk -v day=&quot;$(date)&quot; 'BEGIN { FS=OFS=&quot;;&quot;} { $1=$1 FS day; print}'

P.S. here's some Rogaine - have fun vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
cool, many many thanks. which part determines where to insert the date variable? For example, if i wanted to insert the date after the second semi-colon what would I change? The above inserts it after the first semi-colon.
 
To insert after the SECOND field [;]:

nawk -v day=&quot;$(date)&quot; 'BEGIN { FS=OFS=&quot;;&quot;} { $2=$2 FS day; print}'
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
My oh my oh my,

Thank you very much. vlad, I want to express my sincere appreciation for all your help - it has been excellent.
And the time taken to help us poor souls new to all this stuff - thank you very very much.

How long have you been programming at this level?
 
long time (it's lamentable) [wink]

glad I could help out vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Hi,

Just for fun, with many respects to my &quot;nawk idole&quot; ;-) , is this good too ?

sed -e &quot;s/\(;[^;]*;\)\(.*\)/\1`date +%d-%m-%y`;\2/&quot; <your file name>
 
I don't know who you're referring to [wink] , but.... I'd defer to my &quot;sed idol&quot; - bigoldbulldog.

looks good to me though
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
In fact, I just wanted to add something with &quot;sed&quot; before jamisar tells us to do so

Good God, I'm really bad boy ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top