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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

beginner with awk...pb with gsub...

Status
Not open for further replies.

zoufri75

Technical User
Mar 17, 2002
25
FR
hi everybody!!

i want to write a script whith gsub who substitute the last field into " `date +%m%y` "

my line is like that :

aHT HTBACS HT * 0120 T 0202


could u help me...

thanks a lot!!

 
Hi,

you don't need gsub to make the substitution, just do :
awk '{ $NF = "`date +%m%y`" ; print $0 }' input_file

Jean Pierre.
 
or if you want to substitute by the current date :

awk 'BEGIN {"Date +%m%y" | Date} {$NF = Date; print $0}' input_file Jean Pierre.
 
when i try this :

awk '{ $NF = "`date +%m%y`" ; print $0 }' input_file

i have this result :


aHT HTBACS HT * 0120 T `date +%m%y`

so my shell doesn't execute the command

maybe something is wrong ....
 
i have try :

awk '/HTBACS/ {datedumois='`date +%m%y`' ;
$NF = datedumois ;
print $1"\t\t\t\t\t"$2"\t"$3"\t"$4"\t\t\t"$5,$6,$7 }' fic_htbacs

but i dont have the right date...


i wan't : 0302
but i have 302

i don't understand why.....

 
First a coorection for my last message :
awk 'BEGIN {"date +%m%y" | getline Date} {$NF = Date; print $0}' input_file

With awk (HP-UX) and nawk (SunOS) i do not have your problem, the date printed as "0302".
I think that awk convert datedumois to numeric.
Try :

awk '/HTBACS/ {datedumois='`date +%m%y`' ;
$NF = "" datedumois "";
print $1"\t\t\t\t\t"$2"\t"$3"\t"$4"\t\t\t"$5,$6,$7 }' fic_htbacs Jean Pierre.
 
The problem comes from the date assignment.

After shell substitution, the assignment is :
datedumois=0302 ;
It is an numeric assignment, so it is the same as :
datedumois=302 ;

You must add " arounf the date for an string assignment :
...{ datedumos="'`date +%m%y`'" ; ...

After substitution :
datedumois="0302" ; # String assignment, 0 not lost
datedumois='`date +%m%y`' Jean Pierre.
 
is it possible to replace the old line by the new line generate by the awk command .

my program looks like that now :

datedumois=`date +%m%y`
date_fic=` grep HTBACS fic_htbacs | awk '{print $7}'`

if [[ $date_fic = $datedumois ]]
then
echo "le fichier n'as pas besoin d etre mis a jour" > htbacs.log
break
else
awk '/HTBACS/ {datedumois="'`date +%m%y`'" ; $NF = datedumois ;
print $1"\t\t\t\t\t"$2" "$3"\t "$4"\t\t\t "$5,$6,$7 }' fic_htbacs >> fic_htbacs

fi
~
 


awk '/HTBACS/{ .... }
$0 !~/HTBACS/ ' > /tmp/fic_htbacs.$$
[ $? -eq 0 ] && mv /tmp/fic_htbacs.$$ fic_htbacs

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top