Como usar grep -f e perl para substituir arquivo embutido substituindo novas linhas por ";X"

Como usar grep -f e perl para substituir arquivo embutido substituindo novas linhas por ";X"

Eu tenho o seguinte arquivo:

$$ 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

Estou tentando alterar todas as linhas que começam com as linhas deste outro arquivo:

$$ cat change_these_lines_only.txt
211
304
310
328
342

Então as linhas terão ";X" no final. Por exemplo, a primeira linha:

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

seria:

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

Eu tentei o seguinte comando:

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"

Mas claramente não está funcionando:

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.
(...)

Eu tentei muitas modificações deste mesmo comando, mas sem sorte. Alguém pode me indicar a direção certa? Muito obrigado!

Responder1

Não tenho muita experiência com perl, desculpe.

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

Responder2

Usando awk mastem que redirecionar a saídapara algum outro arquivo:

Desta forma podemos evitarpara loop e gatocomando

$ 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

informação relacionada