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 > How to use grep function to match word
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to use grep function to match word

Réponse
 
LinkBack Outils de la discussion
Vieux 18/08/2006, 16h51   #1
Bon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to use grep function to match word

Dear all

I tried to use grep function in HP UNIX 11.0 for matching exact word in
a reference file. It not only returns exact matched word, but also
returns similar matched word.

My reference file:
abc20060812 code01
abc_def20060812 code02
xyz_qpr_sty20060812 code03

I tried:
for f in *.txt
{
key=${f%%2*}
mapped_code='grep $key $referencefile | cut -f2 -d" "'
printf "%s%s\n" $mapped_code $(date +%y%m%d)
....
}
done

I want to output:
code01060812 (in file 1 header)
code02060812 (in file 2 header)

But, it output:
code01code02060812 (in file1and file2)

I also tried:
mapped_code='grep -w $key $referencefile | cut -f2 -d" "'
or
mapped_code='grep \<$key\> $referencefile | cut -f2 -d" "'

However, it generates error and cannot match the exact word
respectively. I read other news in google groups. I found grep -w and
grep \<$key\> does not work in HP UNIX.

Could you give me some suggestions?

Cheers
Bon

  Réponse avec citation
Vieux 18/08/2006, 17h09   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to use grep function to match word

On 2006-08-18, Bon wrote:
> Dear all
>
> I tried to use grep function in HP UNIX 11.0 for matching exact word in
> a reference file. It not only returns exact matched word, but also
> returns similar matched word.
>
> My reference file:
> abc20060812 code01
> abc_def20060812 code02
> xyz_qpr_sty20060812 code03
>
> I tried:
> for f in *.txt


You have forgotten 'do'.

> {
> key=${f%%2*}
> mapped_code='grep $key $referencefile | cut -f2 -d" "'
> printf "%s%s\n" $mapped_code $(date +%y%m%d)
> ...
> }
> done
>
> I want to output:
> code01060812 (in file 1 header)
> code02060812 (in file 2 header)
>
> But, it output:
> code01code02060812 (in file1and file2)
>
> I also tried:
> mapped_code='grep -w $key $referencefile | cut -f2 -d" "'
> or
> mapped_code='grep \<$key\> $referencefile | cut -f2 -d" "'
>
> However, it generates error and cannot match the exact word
> respectively. I read other news in google groups. I found grep -w and
> grep \<$key\> does not work in HP UNIX.


for f in *.txt
do
key=${f%%2*}
grep "$key" "$referencefile" |
while read a mapped_code
do
printf "%s%s\n" "$mapped_code" "$(date +%Y%m%d)"
done
done


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  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 12h46.


É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 2,16228 seconds with 10 queries