|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|