grep 스크립트 - 동시에 에코할 출력 라인

grep 스크립트 - 동시에 에코할 출력 라인

내가 만든 간단한 스크립트를 개선하고 싶습니다.

단일 매개변수에 대해서는 제대로 실행되고 원하는 대로 작동하지만 모든 매개변수 값에 대해 병렬로 또는 동시에 실행하는 데 몇 가지 문제가 있습니다. 여러 인수로 실행하고 grep 결과를 순차적이 아닌 동시에 출력하도록 이를 개선하고 싶지만 이것이 좋은 선택이 아닐까요? 이 출력 작업을 수행하는 데 도움을 주시면 대단히 감사하겠습니다. 매우 감사합니다.

  • 여러 파일이 있습니다. file1.log file1.txt file2.log file2.txt
  • *.log 및 *.txt에서 일부 콘텐츠를 수집해야 합니다.
  • 모든 매개변수에 대한 grep 행을 동일한 에코로 출력합니다.

지금까지 내 스크립트는 다음과 같습니다.

#!/bin/bash

filename=$@



error=$(grep  'ERROR' ${filename}.l)
phone=$(grep 'phone'  ${filename}.e)
invalid=$(grep  'invalid' ${filename}.l)

while true ; do 

echo -e  " Start of message \n :  
         $error \n
         $invalid \n
        $phone \n
          End of message \n "

break 
done 
exit

이것이 내가 원하는 출력 결과입니다.

Start of message 

error form  file1
Phone number from file1
Invalid from file1

error form  file2
Phone number from file2
Invalid from file2

error form  file3
Phone number from file3
Invalid from file3 

etc 

End of message 

답변1

$@는 문자열이 아닌 배열이므로 실제로 원하는 것은 루프를 사용하여 배열을 반복하는 것입니다. 이 시도:

#!/bin/bash
for filename in "$@"; do
   error=$(grep  'ERROR' "${filename}.l")
   phone=$(grep 'phone'  "${filename}.e")
   invalid=$(grep  'invalid' "${filename}.l")
   echo -e  " Start of message \n :
      $error \n
      $invalid \n
      $phone \n
      End of message \n "
done 
exit 

답변2

echo "Start of message "

for file in "$@"
do

error=$(grep  'ERROR' ${file}.l)
phone=$(grep 'phone'  ${filee}.e)
invalid=$(grep  'invalid' ${file}.l)

echo -e  "${error} from ${file}\n ${phone} from ${file}\n  $invalid from ${file}\n\n"

done
echo -e "End of message \n"

관련 정보