Problema con el comando awk

Problema con el comando awk

Tengo un script que arroja el siguiente error. Cualquiera puede rastrearlo. Estoy atascado en esto durante las últimas 4 horas.

El script contiene lo siguiente: vi bb.sh

#!/bin/awk -f
'BEGIN{FS=OFS="|"} NR==FNR{$1="";++a[$0];next} {field1=$1;$1=""; if ( !(a[$0]) ) {$1=field1;print $0} }' /home/path/a.txt  /home/path/b.txt >  /home/path/c.txt

error después de la ejecución:

-bash-4.2$ sh bb.sh
bb.sh: line 2: BEGIN{FS=OFS="|"} NR==FNR{$1="";++a[$0];next} {field1=$1;$1=""; if ( !(a[$0]) ) {$1=field1} }: command not found

Respuesta1

Te falta awken el guión:

awk 'BEGIN{FS=OFS="|"} NR==FNR{$1="";++a[$0];next} {field1=$1;$1=""; if ( !(a[$0]) ) {$1=field1;print $0} }' \
/home/path/a.txt  /home/path/b.txt >  /home/path/c.txt

El guión probablemente también debería tener #!/bin/shcomo primera línea.

Alternativamente, convertirá el script en un awkscript adecuado:

#!/usr/bin/awk -f

BEGIN {FS = OFS = "|"} 

NR==FNR { $1="";++a[$0]; next }

{
  field1 = $1;
  $1 = ""; 
  if ( !(a[$0]) ) {
     $1 = field1;
     print $0;
  }
}

... y luego ejecutarlo con, por ejemplo

$ ./bb.awk /home/path/a.txt /home/path/b.txt >/home/path/c.txt

Respuesta2

Tiene un awkscript pero está intentando ejecutarlo como shscript. Eso no funcionará. Tienes dos opciones:

  1. Ejecútelo como un script awk. Dado que su archivo ya tiene una línea shebang ( #!/bin/awk -f), simplemente elimine las comillas y los archivos de entrada:

    #!/bin/awk -f
    BEGIN{FS=OFS="|"} NR==FNR{$1="";++a[$0];next} {field1=$1;$1=""; if ( !(a[$0]) ) {$1=field1;print $0} }
    

    Luego, hazlo ejecutable ( chmod a+x bb.sh) y ejecútalo:

    ./bb.sh  /home/path/a.txt  /home/path/b.txt >  /home/path/c.txt
    

    También es posible que desees eliminar la .shextensión o cambiarle el nombre .awkpara que no te confunda. A la computadora no le importará, eso está ahí para ti.

  2. Conviértalo en un script de shell que ejecute el awkcomando:

    #!/bin/sh
    awk 'BEGIN{FS=OFS="|"} NR==FNR{$1="";++a[$0];next} {field1=$1;$1=""; if ( !(a[$0]) ) {$1=field1;print $0} }' /home/path/a.txt  /home/path/b.txt >  /home/path/c.txt
    

información relacionada