Bash: 검색 패턴을 기반으로 여러 로그 파일 구성 결합

Bash: 검색 패턴을 기반으로 여러 로그 파일 구성 결합

많은 txt 파일이 포함된 폴더가 있습니다. 각 파일은 다음 형식으로 존재합니다.

Allowed overlap: -3
H-bond overlap reduction: 0.4
Ignore contacts between atoms separated by 4 bonds or less
Detect intra-residue contacts: False
Detect intra-molecule contacts: False

19 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335

내 목표: 폴더의 파일을 반복하여 이를 결합하여 전역 출력으로 만드는 것입니다. 예제에서는 "19(이 숫자는 파일마다 다름) 연락처" 뒤의 문자열만 고려하여 파일의 처음 6줄을 건너뛰고 싶다는 점에 주목할 필요가 있습니다.

구현에 가능한 작업 흐름:

# make a log file which will contain info from all files going to be looped on the next step.
echo "This is a beginning of the global output" > ./final_output.txt
# that is a key phrase which is the indicator of the first string which should be taken from each of the files
key= "#any of the digit# contacts" 

#now I want to loop each of the files with the aim to add all of the strings after (and including) ${key} to the final_output.txt
for file in ${folder}/*.txt; do
  file_title=$(basename "$file")
  # 1- print the ${file_title} within the final_output.txt
  # 2 -  add all of the strings from the file into the final_output.txt
  # NB ! I need to take only the strings after (and including) the key-phrace

done

답변1

3개의 파일을 예로 들어보겠습니다.

파일 1

Allowed overlap: -3
H-bond overlap reduction: 0.4
Ignore contacts between atoms separated by 4 bonds or less
Detect intra-residue contacts: False
Detect intra-molecule contacts: False

19 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335

파일 3

Allowed overlap: -3
H-bond overlap reduction: 0.4
Ignore contacts between atoms separated by 4 bonds or less
Detect intra-residue contacts: False
Detect intra-molecule contacts: False

17 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335

파일 4

Allowed overlap: -3
H-bond overlap reduction: 0.4
Ignore contacts between atoms separated by 4 bonds or less
Detect intra-residue contacts: False
Detect intra-molecule contacts: False

12 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335

다음 코드는 19개 연락처, 17개 연락처, 12개 연락처의 출력을 파일 끝까지 저장합니다.

 for i in file1 file3 file4; do sed -n '/^[0-9]/,$p'  $i; done > /var/tmp/outputfile.txt

산출

19 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335
17 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335
12 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335

답변2

동일한 입력 파일을 사용하여 다른 방법을 찾았습니다.

암호:

 for i in file1 file3 file4; do sed '1,6d'  $i; done > /var/tmp/outputfile.txt

산출

19 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335
17 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335
12 contacts
atom1  atom2  overlap  distance
:128.B@BB  :300.C@BB  -1.676  4.996
:179.B@BB  :17.C@BB   -1.898  5.218
:182.B@BB  :17.C@BB   -2.015  5.335

관련 정보