Cómo usar grep -f y perl para reemplazar archivos en línea sustituyendo nuevas líneas con ";X"

Cómo usar grep -f y perl para reemplazar archivos en línea sustituyendo nuevas líneas con ";X"

Tengo el siguiente archivo:

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

Estoy intentando cambiar todas las líneas que comienzan con las líneas de este otro archivo:

$$ cat change_these_lines_only.txt
211
304
310
328
342

Entonces las líneas tendrán ";X" al final. Por ejemplo, la primera línea:

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

sería:

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

Probé el siguiente 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"

Pero claramente no funciona:

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

Probé muchas modificaciones de este mismo comando, pero no tuve suerte. ¿Alguien puede indicarme la dirección correcta? ¡Muchas gracias!

Respuesta1

No tengo mucha experiencia con Perl, lo siento.

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

Respuesta2

Usando awk perotener que redirigir la salidaa algún otro archivo:

De esta manera podemos evitarpara bucle y gatodominio

$ 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

información relacionada