How to delete all lines except lines that include

2019-07-15 12:31发布

I have created a script that formats all of my PL/SQL files into a simple file that has pairs of values, its quite hard to explain, i think it will be easier if you just look at the file;

PROCEDURE VALIDA_CAMBIO_GPR
TRUNCATE TMP_MOD_PVA
INSERT TMP_MOD_PVA
PROCEDURE AJUSTAR_FECHAS
INSERT PRO_TDA_VARLOG_ALM
PROCEDURE DEPURAR_CAMBIOS_GPR
PROCEDURE INC_EX_0001
INSERT CABECERA_ALARMAS
PROCEDURE INC_EX_0002
INSERT CABECERA_ALARMAS
PROCEDURE INC_EX_0003
INSERT CABECERA_ALARMAS
PROCEDURE INC_EX_0005
INSERT CABECERA_ALARMAS
PROCEDURE INC_EX_0007
INSERT CABECERA_ALARMAS
PROCEDURE INC_EX_0008
INSERT CABECERA_ALARMAS
PROCEDURE INC_EX_0009
INSERT CABECERA_ALARMAS
PROCEDURE INC_EX_0010
INSERT CABECERA_ALARMAS
PROCEDURE INC_EX_0011
INSERT CABECERA_ALARMAS

Ok, this file goes on for 4000 lines, but i dont need all of the lines, all i want are the pairs of TRUNCATE and INSERTS of the same table, so for example out of the last file i would only want to keep the name of the procedure containing the pair, and the pair. f.e

PROCEDURE VALIDA_CAMBIO_GPR
TRUNCATE TMP_MOD_PVA
INSERT TMP_MOD_PVA

As you can see, the TRUNCATE and INSERT affect the same table, there may also be more than one pair per procedure. How could i do this? I DO NOT WANT all the procedure's that dont have these pairs, and i also don't want any truncates of inserts that don't have their pair. Sorry for the bad explanation, if oyu have problems understanding just comment and i'll do my best to upgrade.

PD: Im using ksh.

标签: shell ksh
1条回答
狗以群分
2楼-- · 2019-07-15 12:50

Using awk:

awk '$1=="PROCEDURE"{p=$0;next} p && $1=="TRUNCATE"{t=$2;next} t==$2 && $1=="INSERT"{
     print p; print "TRUNCATE" t; print "INSERT" t; print ""; t=""}' file
PROCEDURE VALIDA_CAMBIO_GPR
TRUNCATETMP_MOD_PVA
INSERTTMP_MOD_PVA
查看更多
登录 后发表回答