PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > awk
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

awk

Réponse
 
LinkBack Outils de la discussion
Vieux 22/08/2006, 19h27   #1
AL
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut awk


Why in the following code?

varA=2;

strA="`cat /root/mytempdir/wordlist.txt | awk '{print $ 2}'`"
strB="`cat /root/mytempdir/wordlist.txt | awk '{print $ $varA}'`"

strA contains the second word of line 1
and strB contains all line 1
???

Thanks



  Réponse avec citation
Vieux 22/08/2006, 19h53   #2
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk

AL wrote:
> Why in the following code?
>
> varA=2;
>
> strA="`cat /root/mytempdir/wordlist.txt | awk '{print $ 2}'`"
> strB="`cat /root/mytempdir/wordlist.txt | awk '{print $ $varA}'`"
>
> strA contains the second word of line 1
> and strB contains all line 1
> ???


shell variables within single-quotes are not interpolated. try
enclosing the awk statements with double quotes.

strB=`cat /root/mytempdir/wordlist.txt | awk "{print $ $varA}"`

--
XC

  Réponse avec citation
Vieux 22/08/2006, 20h11   #3
AL
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk

Thank you !





  Réponse avec citation
Vieux 22/08/2006, 20h31   #4
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk

Xicheng Jia wrote:

> AL wrote:
>
>>Why in the following code?
>>
>>varA=2;
>>
>>strA="`cat /root/mytempdir/wordlist.txt | awk '{print $ 2}'`"
>>strB="`cat /root/mytempdir/wordlist.txt | awk '{print $ $varA}'`"
>>
>>strA contains the second word of line 1
>>and strB contains all line 1
>>???

>
>
> shell variables within single-quotes are not interpolated.


Right, so to awk "$var" is trying to use an awk (NOT shell) variable
varA to access a positional parameter. Since varA is an unitialised awk
variable, it gets initialised on first use to the value zero, to $varA
is the same as $0 which is the whole input record.

try
> enclosing the awk statements with double quotes.
>
> strB=`cat /root/mytempdir/wordlist.txt | awk "{print $ $varA}"`


No, don't do that as you'd leave yourself open to obscure failures if
your variable isn't set as expected.

Instead set an awk variable based on the value of the shell variable, e.g.:

awk -v varA="$varA" '{print $varA}'

See question 24 in the FAQ,
http://home.comcast.net/~j.p.h/cus-faq-2.html#24, for more details.

Ed
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h59.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08758 seconds with 12 queries