Afficher un message
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
 
Page generated in 0,55686 seconds with 9 queries