按顺序打印结果

时间:2019-04-08 16:18:09

标签: linux bash shell

我有一个输入文件txt,就像这样

999844634|715717657|1508241298     |000018995|INSTALACION DECO + PUNTO TV ADIC DIGITAL|000000001|ALTA                                    |73479107                                |2019-03-15|246221122|0001|671564720|002|DNI|02842909            |
999844634|715717657|1508241298     |000021932|DECODER HD ZAPPER COMODATO              |000000001|ALTA                                    |73479107                                |2019-03-15|246221122|0001|671564720|002|DNI|02842909            |
999844634|715717657|1508241298     |000021932|DECODER HD ZAPPER COMODATO              |000000001|ALTA                                    |73479107                                |2019-03-15|246221122|0001|671564720|002|DNI|02842909            |
999846100|745304617|1501278792     |000018995|INSTALACION DECO + PUNTO TV ADIC DIGITAL|000000001|ALTA                                    |12544155                                |2019-03-15|248309282|0002|774235318|003|DNI|29600747            |
999846100|745304617|1501278792     |000021148|BLOQUE CATV FULL HD                     |000000001|ALTA                                    |12544155                                |2019-03-15|248309282|0002|774235318|003|DNI|29600747            |
999846100|745304617|1501278792     |000021251|DECODIFICADOR SD TV VENTA CATV          |000000046|BAJA MIGRACION DE P/S                   |12544155                                |2019-03-15|248309282|0001|774235318|003|DNI|29600747            |
999846100|745304617|1501278792     |000021956|INSTALACION PUNTO TV ANALOGICO ALTA     |000000046|BAJA MIGRACION DE P/S                   |12544155                                |2019-03-15|248309282|0001|774235318|003|DNI|29600747            |

并且我有一个脚本可以转换这些数据

    system=ATIS
    username=AUTOMATICO
    fecha=$(date +"%Y%m%d_%H%M%S")
    header="$system|$file|$fecha|$username|"
    #echo $header


    ## quitar espacios tr -s " "
    ## concatenar sed -e 's/^/'$header'/g'
    ## cadena awk -F'|' '{print $6"|"$7"|"$14"|"$15"|"$8"|"$4}'


    #### ALTA

    operacion="ALTA|"

    temp1=$(cat $file | grep -i -e '001.*ALTA' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )

    #### ELIMINAR

        operacion="ELIMINAR|"

  temp2=$(cat $file | grep -i -e '046.*BAJA FINAL' -e '008.*BAJA FINAL' -e '012.*BAJA FINAL' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )
    #### BAJA

        operacion="BAJA|"

    temp3=$(cat $file | grep -i -e '008.*BAJA APC' -e '046.*BAJA MIGRACION' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )

    #### ACTIVAR

        operacion="ACTIVAR|"

        temp4=$(cat $file | grep -i -e '027.*RECONEXION APC' -e '028.*RECONEXION DEUDA' -e '042.*RECONEXION TIPIFICADA' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )

    #### DESACTIVAR

    operacion="DESACTIVAR|"

    temp5=$(cat $file | grep -i -e '031.*SUSPENSION APC' -e '032.*SUSPENSION PARCIAL DEUDA' -e '033.*SUSPENSION TOTAL DEUDA' -e '040.*SUSPENCION TIPIFICADA PARCIAL' -e '041.*SUSPENCION TIPIFICADA TOTAL' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )

    #### MANTENER

    temp6=$(cat $file | grep -i -e '046.*ALTA - BAJA  MIGRACION' -e '047.*ALTA - BAJA  MIGRACION' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )

#### CAMBIO TITULARIDAD

        operacion="CAMBTITU|"

    temp7=$(cat $file | grep -i -e '018.*CAMBIO DE TITULAR' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )


    ##armar los archivos

    #echo "$temp1" > ATIS_ALTA_temp.txt
    #echo "$temp2" > ATIS_ELIMINAR_temp.txt
    #echo "$temp3" > ATIS_BAJA_temp.txt
    #echo "$temp4" > ATIS_ACTIVAR_temp.txt
    #echo "$temp5" > ATIS_DESACTIVAR.txt

    echo "$temp1" > ${system}_ALL.txt
    echo "$temp2" >> ${system}_ALL.txt
    echo "$temp3" >> ${system}_ALL.txt
    echo "$temp4" >> ${system}_ALL.txt
    echo "$temp5" >> ${system}_ALL.txt
    echo "$temp7" >> ${system}_ALL.txt

在此数据输出中

ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|02842909|73479107|000018995
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|02842909|73479107|000021932
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|02842909|73479107|000021932
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|29600747|12544155|000018995
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|29600747|12544155|000021148
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|BAJA|DNI|29600747|12544155|000021251
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|BAJA|DNI|29600747|12544155|000021956

但是我希望我的输出与收入保持相同的顺序,因为有时数据太多,过滤器弄乱了,我该如何按输入顺序来写输出?

1 个答案:

答案 0 :(得分:0)

IIUC,您想将一堆模式映射到不同的操作中。如果是这样,则可以将awk与以下代码示例一起使用:

$ cat t18.awk
# here we set up "|" and optional surrounding spaces as FS, so no need 
# to run sed to remove extra spaces
# OFS="|", header will be feed from the command line with -v option
BEGIN{ FS = " *\\| *"; OFS = "|"; }

# use if/else to find operation based on pattern in your "grep" commands
# I just added three, you need to add rest of them to the function
function find_operation() {
    if (/001.*ALTA/) return "ALTA"
    else if (/(046|008).*BAJA FINAL/) return "ELIMINAR"
    else if (/008.*BAJA APC|046.*BAJA MIGRACION/) return "BAJA"
    else return ""
}

# run the function(), if operation is not EMPTY, print it
operation = find_operation() {
    print header, operation, $14, $15, $8, $4
}

我在MacOS上运行以上命令,结果如下:

$ header="ATIS|testArnold2.txt|data|atok"
$ awk -v header="$header" -f t18.awk file.txt 
ATIS|testArnold2.txt|data|atok|ALTA|DNI|02842909|73479107|000018995
ATIS|testArnold2.txt|data|atok|ALTA|DNI|02842909|73479107|000021932
ATIS|testArnold2.txt|data|atok|ALTA|DNI|02842909|73479107|000021932
ATIS|testArnold2.txt|data|atok|ALTA|DNI|29600747|12544155|000018995
ATIS|testArnold2.txt|data|atok|ALTA|DNI|29600747|12544155|000021148
ATIS|testArnold2.txt|data|atok|BAJA|DNI|29600747|12544155|000021251
ATIS|testArnold2.txt|data|atok|BAJA|DNI|29600747|12544155|000021956

当然,您应该将标头更改为代码中定义的标头,只需删除尾随管道|