gpt4 book ai didi

linux - 按顺序打印结果

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:42 25 4
gpt4 key购买 nike

我有一个输入文件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

但我希望我的输出与收入的顺序相同,因为有时它有太多的数据和过滤器搞砸了,我如何按照输入的相同顺序编写它?

最佳答案

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

当然,您应该将 header 更改为您在代码中定义的内容,只需删除尾部管道 |

关于linux - 按顺序打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55577678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com