두 개의 입력 파일을 반복하는 방법

두 개의 입력 파일을 반복하는 방법

나는 awk를 사용하여 두 파일을 일치시킨 다음 file2와 file1의 요소를 곱합니다.

 awk 'NR == FNR{a[$1]=$2; b[$1]=$3; next}
      /:/ || !NF{print; next}
     {print $1, $2*a[$1], $2*b[$1]}' file2 file1 > output

스크립트는 두 개의 입력 파일만 처리하고 하나의 출력 파일을 생성합니다.

나는 이 스크립트를 많은(수천)개의 파일과 함께 사용하기 위해 루프를 만들고 싶습니다. 나는 이것을 시도한다:

for file1 in ../mo/*e.log | 
for file2 in ../excited/*-d.log;   do
awk 'NR == FNR{a[$1]=$2; b[$1]=$3; next}
     /:/ || !NF{print; next}
     {print $1, $2*a[$1], $2*b[$1]}' "$file1" "$file2" > "${file1%e.log}f.log"
done

파일은 서로 관련되어 있으므로 0001e.log 및 0001-d.log, 0002e.log 및 0002-d.log, 0002e.log 및 0002-d.log... 예상되는 출력은 0001f.log, 0002f일 수 있습니다. .로그, 0003f.로그 ...

그러나 성공하지 못했습니다. 어떤 아이디어가 있나요?

답변1

아마도 당신은 다음을 원할 것입니다:

set ../mo/*e.log
for file2 in ../excited/*-d.log; do
  file1=$1; shift
  awk 'NR == FNR{a[$1]=$2; b[$1]=$3; next}
       /:/ || !NF{print; next}
       {print $1, $2*a[$1], $2*b[$1]}' "$file1" "$file2" > "${file1%e.log}f.log"
done

또는 다음을 사용하여 zsh:

file1s=(../mo/*e.log)
file2s=(../excited/*-d.log)
for file1 file2 (${file1s:^file2s}) {
  awk 'NR == FNR{a[$1]=$2; b[$1]=$3; next}
       /:/ || !NF{print; next}
       {print $1, $2*a[$1], $2*b[$1]}' "$file1" "$file2" > "${file1%e.log}f.log"
}

위에서는 두 개의 정렬된 파일 이름 목록이 있으며 두 목록을 동시에 탐색합니다. in 및 in 파일의 기본 이름을 일치시키려면 mo다음을 수행할 수 있습니다.excited

for file1 in ../mo/*e.log; do
  basename=${file1%e.log}
  basename=${basename##*/}
  file2=../excited/$basename-d.log
  [ -f "$file2" ] || continue
  awk 'NR == FNR{a[$1]=$2; b[$1]=$3; next}
       /:/ || !NF{print; next}
       {print $1, $2*a[$1], $2*b[$1]}' "$file1" "$file2" > "${file1%e.log}f.log"
done

답변2

노력하다 paste file1 file2 | tr '\t' '*' | bc > output.

그런 다음 큰 루프의 경우(세게 때리다), 이는 파일을 다음에서 변경합니다.../월/,../흥분한/, 그리고 제품을 다음으로 출력합니다.에프현재 디렉토리에 있는 일련의 번호가 매겨진 파일:

for f in ../mo/*e.log; do
    g=${f/mo/excited}
    o=${f##*/}
    paste $f ${g/e.log/-d.log} | tr '\t' '*' | bc > ${o/e.log/f.log} 
done

데모(와 함께세게 때리다isms), 1-5의 제곱을 인쇄합니다.

paste <(seq 5) <(seq 5) | tr '\t' '*' | bc

산출:

1
4
9
16
25

답변3

GNU Parallel이 설치되어 있으면 다음을 수행할 수 있습니다.

doit() {
  file1="$1"
  file2="$2"
  output="$3"
  awk 'NR == FNR{a[$1]=$2; b[$1]=$3; next}
      /:/ || !NF{print; next}
     {print $1, $2*a[$1], $2*b[$1]}' "$file2" "$file1" > "$output"
}
export -f doit

# If all filenames fit on a command line:
parallel --xapply doit {1} {2} {1/.}{2/.}.out ::: ../mo/?*e.log ::: ../excited/?*d.log
# With newer versions you can do:
parallel  doit {1} {2} {1/.}{2/.}.out ::: ../mo/?*e.log :::+ ../excited/?*d.log

# If you do not like the {/.} you can do:
parallel doit {1} '{= s/e.log/d.log/;s:/mo/:/excited/:; =}' '{=s/.log/.out/;s:^../mo/::;=}' ::: ../mo/?*e.log

# If all the files do not fit on the command line (because you have thousands):
finda() { find ../mo/ -name '*e.log'; }
findb() { find ../excited/ -name '*d.log'; }

parallel --xapply doit {1} {2} {1/.}{2/.}.out :::: <(finda) <(findb)
parallel doit {1} {2} {1/.}{2/.}.out :::: <(finda) ::::+ <(findb)
finda | parallel doit {1} '{= s/e.log/d.log/;s:/mo/:/excited/:; =}' '{=s/.log/.out/;s:^../mo/::;=}'

각 코어는 하나의 작업을 실행합니다. 한 번에 하나의 작업을 수행하려면 parallel로 바꾸십시오 parallel -j1.

GNU Parallel은 동일한 컴퓨터 또는 SSH를 통해 액세스할 수 있는 여러 컴퓨터에서 작업을 병렬로 쉽게 실행할 수 있게 해주는 범용 병렬 처리기입니다. 종종 for루프를 대체할 수 있습니다.

4개의 CPU에서 32개의 서로 다른 작업을 실행하려는 경우 병렬화하는 간단한 방법은 각 CPU에서 8개의 작업을 실행하는 것입니다.

간단한 스케줄링

대신, GNU Parallel은 작업이 완료되면 새로운 프로세스를 생성하여 CPU를 활성 상태로 유지하여 시간을 절약합니다.

GNU 병렬 스케줄링

설치하다

배포판에 GNU Parallel이 패키지되어 있지 않으면 루트 액세스 없이 개인 설치를 수행할 수 있습니다. 이 작업은 10초 안에 완료할 수 있습니다.

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash

다른 설치 옵션은 다음을 참조하세요.http://git.savannah.gnu.org/cgit/parallel.git/tree/README

더 알아보기

더 많은 예시 보기:http://www.gnu.org/software/parallel/man.html

소개 비디오 보기:https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

이 튜토리얼을 살펴보세요:http://www.gnu.org/software/parallel/parallel_tutorial.html

지원을 받으려면 이메일 목록에 가입하세요.https://lists.gnu.org/mailman/listinfo/parallel

관련 정보