Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[RISOLTO]awk e formattazione numeri
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano
View previous topic :: View next topic  
Author Message
silvius
Guru
Guru


Joined: 16 May 2006
Posts: 364

PostPosted: Wed Sep 20, 2006 4:41 pm    Post subject: [RISOLTO]awk e formattazione numeri Reply with quote

Sto scrivendo uno script usando al suo interno awk per estrarre delle stringhe.
Con awk è tutto ok, però mi trovo $1 con un numero 1234.1234, ed io vorrei formattarlo in 1234.12 ( od averlo già formattato nell' $1 ).
In sostanza devo togliere solo dei decimali.

In bash ho sempre usato

Code:
echo "scale=2; 1234.1234/1" | bc


ma se inserisco nel mio script:

Code:
NuovaVar=$(echo "scale=2; $1/1" | bc)


Non funziona, lo so che è una domanda stupida, però sono agli inizi del bash scripting


Saluto


Last edited by silvius on Sat Sep 23, 2006 3:32 pm; edited 1 time in total
Back to top
View user's profile Send private message
codadilupo
Advocate
Advocate


Joined: 05 Aug 2003
Posts: 3135

PostPosted: Wed Sep 20, 2006 5:27 pm    Post subject: Reply with quote

Code:
NuovaVar=`echo "scale=2;" $A1"/1" | bc`
:?:

Coda
Back to top
View user's profile Send private message
silvius
Guru
Guru


Joined: 16 May 2006
Posts: 364

PostPosted: Wed Sep 20, 2006 5:54 pm    Post subject: Reply with quote

codadilupo wrote:
Code:
NuovaVar=`echo "scale=2;" $A1"/1" | bc`
:?:

Coda


Mi funziona in shell ma NON funziona se lo inserisco in uno script, perchè mi dice invalid char alla " ' inversa".

Saluto
Back to top
View user's profile Send private message
codadilupo
Advocate
Advocate


Joined: 05 Aug 2003
Posts: 3135

PostPosted: Wed Sep 20, 2006 5:56 pm    Post subject: Reply with quote

posta lo script intero, che non son buono coi cruciverba ;-)

Coda
Back to top
View user's profile Send private message
silvius
Guru
Guru


Joined: 16 May 2006
Posts: 364

PostPosted: Wed Sep 20, 2006 6:08 pm    Post subject: Reply with quote

Richiamo il file sotto esposto con un awk

Code:
awk -f /home/file1 /home/conf.awk > /home/file2


Code:
#!/bin/bash

BEGIN   {
   FS="\n"
   RS="FINE"
   }

{
if ($10 == "TROVATO")
   {
   PrimoArray[NumLinea]=`echo "scale=2;" $1"/1" | bc`
   SecondoArray[NumLinea]=$2
   print "A" PrimoArray[NumLinea] "B" SecondoArray[NumLinea]
   }
}




Saluto
Back to top
View user's profile Send private message
bandreabis
Advocate
Advocate


Joined: 18 Feb 2005
Posts: 2495
Location: イタリアのロディで

PostPosted: Wed Sep 20, 2006 6:37 pm    Post subject: Reply with quote

codadilupo wrote:
posta lo script intero, che non son buono coi cruciverba ;-)

Coda

:lol: :lol: :lol: Sei sempre il migliore!! :lol: :lol: :lol:
_________________
Il numero di post non fa di me un esperto! Anzi!
Back to top
View user's profile Send private message
silvius
Guru
Guru


Joined: 16 May 2006
Posts: 364

PostPosted: Wed Sep 20, 2006 6:40 pm    Post subject: Reply with quote

Penso di aver capito, il file che richiamo deve contenere solo comandi compatibili con awk.............quindi come faccio ?

Saluto
Back to top
View user's profile Send private message
codadilupo
Advocate
Advocate


Joined: 05 Aug 2003
Posts: 3135

PostPosted: Wed Sep 20, 2006 7:12 pm    Post subject: Reply with quote

silvius wrote:
Penso di aver capito, il file che richiamo deve contenere solo comandi compatibili con awk.............quindi come faccio ?

Saluto


in realtà a me da già problemi il semplice bash:

Code:
file1.sh: line 3: BEGIN: command not found
file1.sh: line 6: syntax error near unexpected token `}'
file1.sh: line 6: `         }'


Coda
Back to top
View user's profile Send private message
codadilupo
Advocate
Advocate


Joined: 05 Aug 2003
Posts: 3135

PostPosted: Sat Sep 23, 2006 1:48 pm    Post subject: Reply with quote

hai poi risolto ? sono curioso di sapere com'e' andata.

Coda
Back to top
View user's profile Send private message
silvius
Guru
Guru


Joined: 16 May 2006
Posts: 364

PostPosted: Sat Sep 23, 2006 3:32 pm    Post subject: Reply with quote

codadilupo wrote:
hai poi risolto ? sono curioso di sapere com'e' andata.

Coda


Alla fine c'è l' ho fatta, usando un :

Code:

printf( "%.2f %.2f ", $t,$u)


Come quando usi il c.

[EDIT]

L' unico problema che ho ora è la velocità di awk, penso che awk non sia il massimo in termini di prestazioni.
Lo stesso programmino in vb ci mette 30 sec. mentre in scripting usando awk 120 sec.
Il lavoro grosso è ordinare in modo crescente dei numeri circa 8000 numeri.
C'è da dire che vb è compilato mentre in bash è interpretato, però mi aspettavo di più.
Comunque cercherò di migliorare.

Saluto
Back to top
View user's profile Send private message
Luca89
Advocate
Advocate


Joined: 27 Apr 2005
Posts: 2107
Location: Agrigento (Italy)

PostPosted: Sat Sep 23, 2006 4:22 pm    Post subject: Reply with quote

Comunque i script awk devono iniziare con:
Code:
#!/bin/awk -f

non con:
Code:
#!/bin/bash


Quote:
C'� da dire che vb � compilato mentre in bash � interpretato, per� mi aspettavo di pi�.


sempre di awk devi parlare, bash non c'entra nulla.
_________________
Running Fast!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum