여러 파일에서 문자열 발생

여러 파일에서 문자열 발생

파일이 두 개 있어요

  1. 입력.txt
  2. 키워드.txt

input.txt다음과 같은 내용이 있습니다.

.src_ref 0 "call.s" 24 first
      0x000000    0x5a80 0x0060         BRA.l 0x60
.src_ref 0 "call.s" 30 first
      0x000002    0x1bc5                RETI
.src_ref 0 "call.s" 31 first
      0x000003    0x6840                MOV R0L,R0L
.src_ref 0 "call.s" 35 first
      0x000004    0x1bc5                RETI

keyword.txt다음과 같은 내용이 있습니다.

MOV
BRA.l
RETI
ADD
SUB
..
etc

이제 이 파일을 읽고 파일 keyword.txt에서 검색하여 몇 번 이나 발생했는지 알아보고 싶습니다 .input.txtMOVBRA.l

지금까지 나는 단일 파일 자체에서 작동하도록 관리했습니다. 여기 코드가 있습니다

#!/usr/bin/perl
use strict;
use warnings;

sub retriver();

my @lines;
my $lines_ref;
my $count;
$lines_ref=retriver();
@lines=@$lines_ref;
$count=@lines;
print "Count :$count\nLines\n";
print join "\n",@lines;

sub retriver()
{
    my $file='C:\Users\vk41286\Desktop\input.txt';
    open FILE, $file or die "FILE $file NOT FOUND - $!\n";
    my @contents=<FILE>;

    my @filtered=grep(/MOV R0L,R0L/,@contents);
    return \@filtered;
}

여기서는 검색만 가능하며 MOV와 같은 다른 지침은 검색할 수 없습니다 RETI.

MOV,RETI또한 등을 파일에 넣고 keyword.txt일반화하고 싶습니다 .

출력은 다음과 같아야 합니다:

MOV has occured 2  times
RETI has occured 1 time

답변1

에 대한 압박이 없다면 perl간단한 명령줄

 grep -f keyword.txt -c input.txt

그것을 해야 한다.

에서는 코드에서 1에 대해서만 했던 것처럼 각 키워드를 perl열고 반복하면서 차례로 파악해야 합니다 .keyword.txt

답변2

bash-script는 다음보다 훨씬 간단한 것 같습니다 perl.

while read keyword
do
    occurrence =$(grep -c -F "$keyword" input.txt)
    echo "$keyword has occurred $occurrence time(s)"
done < keyword.txt

관련 정보