2개의 파일에서 입력을 삽입할 수 있는 bash 스크립트를 찾고 있습니다.

2개의 파일에서 입력을 삽입할 수 있는 bash 스크립트를 찾고 있습니다.

다른 데이터가 포함된 2개의 파일이 있습니다. 어떻게 데이터를 이 파일에 넣고 인쇄할 수 있나요? for 루프를 시도하고 있지만 1개의 변수에서만 작동합니다.

파일 A
https://xyx.com/test-posts/
https://www.abc.com/temp-article/

문서 B
xyx.com
abc.com

나는 아래와 같은 것을 달성하고 싶다

견본

<a href="https://xyx.com/test-posts/">xyx.com</a>
<a href="https://www.abc.com/temp-article/">abc.com</a>

미리 감사드립니다!

답변1

어때요?

$ paste FileA FileB | awk '{print "<a href=\"" $1 "\">" $2 "</a>"}'
<a href="https://xyx.com/test-posts/">xyx.com</a>
<a href="https://www.abc.com/temp-article/">abc.com</a>

참고: 파일 항목에 공백이 포함될 수 있는 경우 좀 더 복잡한 작업을 수행해야 합니다(예: paste및 에 대해 다른 구분 기호 선택 awk). URL이므로 여기서는 그렇지 않은 것 같습니다.

답변2

$ while read -r url && read -r domain <&3; do printf '<a href="%s">%s</a>\n' "$url" "$domain"; done <FileA 3<FileB
<a href="https://xyx.com/test-posts/">xyx.com</a>
<a href="https://www.abc.com/temp-article/">abc.com</a>

또는 스크립트에 다음과 같이 작성할 수 있습니다.

while read -r url && read -r domain <&3; do
    printf '<a href="%s">%s</a>\n' "$url" "$domain"
done <FileA 3<FileB

이는 두 호출 중 어느 쪽도 전체 행을 읽을 수 없을 while때까지의 루프 입니다. read첫 번째는 readURL을 읽고 FileA, 두 번째는 (파일 설명자 3을 통해) read도메인을 읽습니다 FileB.

출력은 printf읽은 데이터를 형식화된 문자열에 삽입하는 호출에 의해 처리됩니다.

답변3

다음 스크립트를 사용하여 이 작업을 수행할 수 있었습니다.

#!/bin/sh

filea=/path/to/filea
fileb=/path/to/fileb

lines=$(grep -c . "$filea")

for ((i=1; i<=lines; i++)); do
    url=$(sed -n "${i}p" "$filea")
    name=$(sed -n "${i}p" "$fileb")
    printf "<a href=\"${url}\">${name}</a>\n"
done

lines총 행 수로 설정됩니다.filea

그런 다음 행 1에서 행 n까지 반복하고 filea합계를 사용하여 각 행에서 행을 추출합니다. 그런 다음 추출된 데이터를 사용하여 필요한 명령문을 인쇄합니다.filebsedhref

관련 정보