grep -f 및 perl을 사용하여 새 줄을 ";X"로 대체하여 파일 인라인을 바꾸는 방법

grep -f 및 perl을 사용하여 새 줄을 ";X"로 대체하여 파일 인라인을 바꾸는 방법

다음 파일이 있습니다.

$$ head on_this_file.txt
Instance,Session,SenderCompID,Type,SrcAddr,SrcPort,DstAddr,DstPort,Protocol,Client,MIC,curr
304,PBAR29,PBAR29,V,146.127.180.96/27,,162.69.138.157,40008,pillar,DESH,ARCX,USD
336,PBAR29,PBAR29,V,146.127.180.64/28,,162.69.138.157,40008,pillar,DESH,ARCX,USD
304,PBAR36,PBAR36,V,146.127.180.96/27,,162.69.142.4,40015,pillar,DESH,ARCX,USD
336,PBAR36,PBAR36,V,146.127.180.64/28,,162.69.142.4,40015,pillar,DESH,ARCX,USD
304,PBAR28,PBAR28,V,146.127.180.96/27,,162.69.142.109,40007,pillar,DESH,ARCX,USD
336,PBAR28,PBAR28,V,146.127.180.64/28,,162.69.142.109,40007,pillar,DESH,ARCX,USD
310,PBAR88,PBAR88,V,146.127.197.128/26,,162.69.142.207,40285,pillar,SQOL,ARCX,USD
346,PBAR88,PBAR88,V,146.127.168.64/27,,162.69.142.207,40285,pillar,SQOL,ARCX,USD
304,PBAR31,PBAR31,V,146.127.180.96/27,,162.69.138.62,40010,pillar,DESH,ARCX,USD

이 다른 파일의 줄로 시작하는 모든 줄을 변경하려고 합니다.

$$ cat change_these_lines_only.txt
211
304
310
328
342

그러면 줄 끝에 ";X"가 표시됩니다. 예를 들어 첫 번째 줄은 다음과 같습니다.

304,PBAR29,PBAR29,V,146.127.180.96/27,,162.69.138.157,40008,pillar,DESH,ARCX,USD

다음과 같습니다:

304,PBAR29,PBAR29,V,146.127.180.96/27,,162.69.138.157,40008,pillar,DESH,ARCX,USD;X

다음 명령을 시도했습니다.

grep -f change_these_lines_only.txt on_this_file.txt | xargs -L1 -I {} sh -c "perl -p -i -e 's/{}\n/{};X\n/' on_this_file.txt"

하지만 분명히 작동하지 않습니다.

Number found where operator expected at -e line 1, near "s/304,PBAR29,PBAR29,V,146.127.180.96/27,,162.69.138.157,40008,pillar,DESH,ARCX,USD\n/304"
Backslash found where operator expected at -e line 1, near "X\"
syntax error at -e line 1, near "s/304,PBAR29,PBAR29,V,146.127.180.96/27,,162.69.138.157,40008,pillar,DESH,ARCX,USD\n/304"
Execution of -e aborted due to compilation errors.
(...)

나는 이 동일한 명령을 여러 번 수정해 보았지만 운이 없었습니다. 누군가 나에게 올바른 방향을 알려줄 수 있습니까? 정말 고마워!

답변1

Perl에 대한 경험이 많지 않습니다. 죄송합니다.

for X in $(cat change_this_lines_only.txt)
do
  sed -i "/^${X},/s/\$/;X/" on_this_file.txt
done

답변2

awk를 사용하지만출력 방향을 다시 지정해야 함다른 파일로:

이렇게 하면 우리는 피할 수 있다for 루프와 고양이명령

$ awk -F"," '
FNR==NR{ a[$1]=$1;next} (FNR==1){print $0;}((NR > 1) && (a[$1]==$1)){ print $0",X"}' change_these_lines_only.txt on_this_file.txt 
Instance,Session,SenderCompID,Type,SrcAddr,SrcPort,DstAddr,DstPort,Protocol,Client,MIC,curr
304,PBAR29,PBAR29,V,146.127.180.96/27,,162.69.138.157,40008,pillar,DESH,ARCX,USD,X
304,PBAR36,PBAR36,V,146.127.180.96/27,,162.69.142.4,40015,pillar,DESH,ARCX,USD,X
304,PBAR28,PBAR28,V,146.127.180.96/27,,162.69.142.109,40007,pillar,DESH,ARCX,USD,X
310,PBAR88,PBAR88,V,146.127.197.128/26,,162.69.142.207,40285,pillar,SQOL,ARCX,USD,X
304,PBAR31,PBAR31,V,146.127.180.96/27,,162.69.138.62,40010,pillar,DESH,ARCX,USD,X

관련 정보