2013-08-22 4 views
-1

두 개의 정렬 된 텍스트 파일이 있습니다. A의 데이터 파일은 다음과 같습니다Bash - 하나의 문자열을 다른 파일의 다른 문자열로 바꿉니다.

adam 
humanities 

antwon 
sciences 

bernard 
economics 

castiel 
sciences 

dmitri 
informatics 

zoe 
mathematics 

파일 B의 데이터는 다음과 같습니다

adamburnston 
antwonreed 
justbernard 
castiel 
dmitrivalchenkov 
zoematthews 

내가 파일 B의 라인 (adamburnston)로, 파일 A (아담)의 라인을 교체해야 . 두 파일 모두 사전 순으로 정렬되며 동일한 수의 항목을 포함합니다. 이 결과는 어떻게 얻을 수 있습니까?

예상 출력 :

adamburnston 
humanities 

antwonreed 
sciences 

justbernard 
economics 

castiel 
sciences 

dmitrivalchenkov 
informatics 

zoematthews 
mathematics 
+0

당신은 무엇을하려고 했습니까? – devnull

+3

귀하의 프로필에 :'Python at the moment'을 배우십시오. 왜 활용하지 않겠습니까? – devnull

+0

@devnull - 'coz bash에서 명시 적으로 요청했습니다. 선택의 여지가 있다면, 파이썬으로 시도해 볼 것입니다. – rahuL

답변

3

다음 파이프 라인 작품 :

sed '2~3!d' A | paste -d $'\n' B - | sed $'3~2i\n' 

첫 번째 부분은 GNU가 두 번째 줄을 인쇄 나오지 다음 각 세 번째 줄 알려줍니다. paste은 개행 문자를 구분 기호로 사용하여 B를 첫 번째 sed의 출력과 병합합니다. 마지막 줄은 각 줄 쌍 뒤에 줄 바꿈을 추가합니다.

2

이 AWK 작동합니다 :

awk 'FNR==NR{a[i++]=$0;next} NF>0{c++; if (c%2==0) print a[j++] ORS $0 ORS}' fileB fileA 
관련 문제