2017-10-31 4 views
1

여러 줄의 텍스트 파일 file1을 가정합니다. 일부 줄에는 키워드 "keyw"이 포함되어 있습니다.소스 및 대상의 위치를 ​​기준으로 awk로 문자열 교체

$ cat file1 
foo 
bar keyw 
baz 
keyw qux 
quux 

는 또한 file1의 키워드 발생 한 많은 문자열을 포함하는 한 줄짜리 텍스트 파일 file2을 가정합니다. file2의 문자열은 단일 공백으로 구분됩니다.

$ cat file2 
string1 string2 

I는 각각의 위치에 기초 file1의 키워드를 포함하는 라인 file2 각각의 문자열을 추가하고자 :

  • file1 첫 번째 줄에 추가 file2의 첫 번째 문자열 그 키워드를 포함합니다.

  • file2의 두 번째 문자열은 키워드가 들어있는 file1의 두 번째 줄에 추가됩니다.

  • 다음

가 추구 출력 :

$ awk ... file1 file2 
foo 
bar keyw string1 
baz 
keyw qux string2 
quux 

무엇 AWK이 교체를 수행하는 데 사용하는 -code?

+2

당신은 무엇을 시도 했습니까? 이전 awk 질문 중 일부가이 주제에 도움이되는 흥미로운 코드로 이어질 것이라고 확신합니다! – fedorqui

+0

귀하의 질문에 25 분 이내에 최선의 최선의 답변을 얻었으므로 더 나은 대답이 게시 될 때까지 기다리는 것이 아니라 받아 들일 수있었습니다. –

답변

0

는 또한 한 줄의 텍스트 파일을 가정

awk

awk ' 
    FNR==NR{split($0,strarr);next} 
    /keyw/{$0 = $0 OFS strarr[++i]}1 
    ' file2 file1 

당신이 말한 이후를 사용하여 위/P O를 원하는 부여합니다 키워드로 많은 문자열을 포함하는 file2 file1의 어커런스. file2의 문자열은 단일 공백으로 구분 된 입니다.이 기본 FS 하나의 공백으로 기록을 나눌 수 있도록

설명

  • split($0,strarr);이 사용되며, 기록 /keyw/이 정규 표현식과 일치 할 때마다 요소는 배열 그래서 strarr

  • 에 저장됩니다 file1의 배열 요소를 인쇄하면 변수 i가 증가하고 다음 줄/레코드로 이동합니다

  • +1은 현재 작업/레코드/행 인 기본 작업을 수행합니다 (print $0). awk의 작동 방식을 알고 싶다면, awk '1' infile, 모든 레코드/라인을 출력하는 반면, awk '0' infile은 아무것도 출력하지 않습니다. 0 이외의 숫자는 으로 기본 동작을 트리거합니다.

테스트 결과 :이 모든입니다

$ cat file1 
foo 
bar keyw 
baz 
keyw qux 
quux 

$ cat file2 
string1 string2 

$ awk 'FNR==NR{split($0,strarr);next}/keyw/{$0 = $0 OFS strarr[++i]}1' file2 file1 
foo 
bar keyw string1 
baz 
keyw qux string2 
quux 
+1

훌륭한 설명 주셔서 감사합니다. –

0

Input_file이 표시된 샘플과 같으면 다음을 시도해보고 도움이 될지 알려 주시기 바랍니다.

awk 'FNR==NR{for(i=1;i<=NF;i++){a[i]=$i}next} {print $0,$0 ~ /keyw/?a[++j]:""}' FIlE2 FIlE1 

출력은 다음과 같습니다.

foo 
bar keyw string1 
baz 
keyw qux string2 
quux 

여기에도 같은 설명을 추가합니다. 한 다음

awk ' 
FNR==NR{   ##Using FNR==NR condition which will be RUE when first Input_file is getting read. FNR and NR both represents number of lines, only difference between them is FNR value will be RESET on every next file is getting read and NR value will be keep on increasing till all the files are read. 
for(i=1;i<=NF;i++){##Starting a for loop which will run from i variable value 1 to till value of variable NF, where NF is out of the box variable whose value is the value of number of fields on a line. 
    a[i]=$i}   ##Creating an array named a whose index is variable i and its value is $i(specific fields value) 
    next    ##next will skip all further statements for current line(s). 
} 
{     ##These statements will be executed when 2nd Input_file is being read. 
    print $0,$0 ~ /keyw/?a[++j]:"" ##Printing the value of current line along with that checking of a line has string keyw in it then printing the value of array a whose index is value of j(whose value increments with 1 each time it comes here), else print NULL/nothing. 
} 
' FIlE2 FIlE1  ##mentioning the Input_file(s) here. 
0

이 필요합니다

awk 'FNR==NR{split($0,a);next} /keyw/{$0=$0 OFS a[++c]} 1' file2 file1 

그것은 어떤 AWK에서 작동 및 비의 끝 부분에 빈 공간을 추가하지 않습니다 타겟 라인.