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

awk modify the structure of the file....!!!!!!!!!

Status
Not open for further replies.

zoufri75

Technical User
Mar 17, 2002
25
FR
my fic is like that :
$more site

r04 VIDOPCLI 04 * 0120
r04 VIDOPCVM 04 * 028
aST VIDSTATS ST * 050
r16 HTEBAC16 16 * 0250 30788 16
aHT HTBACS HT * 0120 T 0302

I want to change the last field of the last line which contains /HTBACS/ to the current date `date +%m%y`
in our case now : "0402" and not modify the strcuture of the file

i have try something like that..but it does'nt respect the structure of the file

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

if [[ $date_fic = $datedumois ]]
then
echo "le fichier n'as pas besoin d etre mis a jour" > site.log
break
else
awk '$0 !~ /HTBACS/ ' site > site.$$
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 }
' site >> site.$$
[ $? -eq 0 ] && mv site.$$ site

fi

if u could help me ...
thanks for all

Zak from Paris


i have also try with sed...sed dont modify the structure of my file but i don't know how to use my variable "datedumois"
 
Zak -

I'm assuming you want

r04 VIDOPCLI 04 * 0120
r04 VIDOPCVM 04 * 028
aST VIDSTATS ST * 050
r16 HTEBAC16 16 * 0250 30788 16
aHT HTBACS HT * 0120 T 0302

to become

r04 VIDOPCLI 04 * 0120
r04 VIDOPCVM 04 * 028
aST VIDSTATS ST * 050
r16 HTEBAC16 16 * 0250 30788 16
aHT HTBACS HT * 0120 T 0402

so in your script try out:

Try out

Code:
sed "\$s/[0-9]\{4\}[    ]*$/`date +%m%y`/" site

The code just replaces the last 4 numbers and any trailing space with you date.

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top