-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshead
More file actions
251 lines (227 loc) · 9.03 KB
/
shead
File metadata and controls
251 lines (227 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/sh
# oxr 2025 SHEAD V2
# Filtra lineas por contenido o pre/sufijo, puede remarcar, borrar o reemplazar,
# contar y definir el modo de salida.
#
# \\r (|) shead (-a archivo) [-o archivo] [-t] [-l|-C] [-S SEP] [-n] ···
# ··· [-f nº|inicioº [finº]] [-z] [-b] [-u] [-P] [-c colorº] [-p|-pp|-s|-ss] ···
# ··· [-|!] [--] filtro [-r reemplazo] [SEP nueva instancia [SEP ...]]
#
# Opciones generales:
# '-a' lee de archivo sino de la salida standard '|', excluyentes entre si
# '-o' salida a otro archivo
# '-t' devolvera todas las lineas en modo pre/sufijo
# '-l' mostrara el nº de linea
# '-C' devolvera el nº de lineas concordantes, si '!' las resultantes; por
# \\r pre/sufijo siempre las concordantes
# '-S' separador, por defecto es '/'
# '-n' sin estilo
# '-f' filtrara las nº ultimas lineas. Ultima opcion general
# 'inicio fin' lineas a examinar, todas por defecto. Ultima opcion general
# Opciones de instancia:
# '-p|-s' filtrara por prefijo o sufijo y actuara sobre ellos, acepta '?'
# '-pp|-ss' idem pero actuara en toda la linea, acepta '?' y '* '
# '-|-q' no mostrara el filtro.
# '!|-N' no mostrara las lineas con coincidencias.
# '-c' da color al filtro o su reemplazo, color[0-7] color/fondo[00-77]
# '-b' no bold
# '-u' subrayado
# '-P' parpadeante
# '-z' no hara saltos de linea. En la ultima instancia
# '--' fin de opciones, puede ser necesario si filtro es un numero.
# 'filtro' texto a encontrar. Entrecomillar si espacios o comodines
# '-r' reemplazar por 'reemplazo'.
#
# Por defecto se muestran todas las lineas y se resaltan las coincidencias.
# Por 'pre/sufijo' se muestran las lineas con coincidencias sin resaltado. Se
# puede filtrar por contenido con '-p *filtro' o '-s filtro*' para mostrar solo
# las lineas coincidentes. Si se indica color 'filtro' no puede tener comodines.
#
#(1) si el archivo tiene una sola linea sin salto de linea, read lee la linea
# y sale del bucle sin ejecutar su contenido.
#
# INCLUIBLE EN OTROS SCRIPTS '. shead'
# EN PROCESO
# Rehacer bloque pre/sufijo mostrar filtro al modo aplicado al de no mostrar,
# compartir el sistema de diferenciacion y reducir las instrucciones tize
[ $include_ctl ] || . include ; include tize isnum path
shead_fun(){
local ln="" tr="" mt=true sp="øo" ft=""
# ln nºlinea - tr texto_restante - mt mostrar - sp separador_para_tize - pt parte
[ ${#shead_ft} -eq 0 ] || { # filtrar
[ $shead_pt = I ] && { # Contenido
tr=${shead_tx##*$shead_ft}
[ ${#shead_rp} -ne 0 ] || {
[ "$shead_tx" != "$tr" ] && { # coincide
case $shead_md in
false) # mostrar
[ ${#shead_cl} -ne 0 ] || shead_cl="-c 2"
shead_nr=$((shead_nr+1))
tize -v shead_tx -s $sp $shead_ne $shead_cl $shead_ft $sp "$shead_tx" ;;
true) # no mostrar el filtro
shead_nr=$((shead_nr+1))
tize -v shead_tx -s $sp $shead_ne -q $shead_ft $sp "$shead_tx" ;;
"") # no mostrar lineas coincidentes
# shead_nr=$((shead_nr+1))
mt=false ;;
esac
} || { [ ${#shead_md} -ne 0 ] || shead_nr=$((shead_nr+1)) ;}
}
} || { # Prefijo/Sufijo
case $shead_pt in
P) tr=${shead_tx#$shead_ft} pt=${shead_tx%"$tr"} ;;
PP) tr=${shead_tx##$shead_ft*} pt=${shead_tx%"$tr"} ;;
S) tr=${shead_tx%$shead_ft} pt=${shead_tx#"$tr"} ;;
SS) tr=${shead_tx%%*$shead_ft} pt=${shead_tx#"$tr"} ;;
esac
[ ${#shead_md} -ne 0 -a "$shead_tx" != "$tr" ] && { # mostrar y no mostrar filtro
[ ${#shead_rp} -ne 0 ] || {
shead_nr=$((shead_nr+1))
$shead_md && shead_cl="-q" || [ ${#shead_cl} -gt 0 ] || shead_ne="-n"
tize -v pt -s $sp $shead_ne $shead_cl $shead_ft $sp "$pt"
[ $shead_pt = P -o $shead_pt = PP ] && shead_tx=$pt$tr || shead_tx=$tr$pt
}
} || { # no mostrar lineas coincidentes - se mostraran lineas no coincidentes
[ ${#shead_md} -eq 0 -a "$shead_tx" = "$tr" ] || { # siempre que no sean true
[ ${#shead_md} -ne 0 ] || shead_nr=$((shead_nr+1))
$shead_mt || mt=false
}
}
}
} # Sin filtro
! $mt || { # reemplazar? y aplicar
# solo pasa si las 2 son false
[ ${#shead_rp} -eq 0 ] || [ "$shead_tx" = "${shead_tx##*$shead_ft}" ] || { # reemplazar
[ ${#shead_cl} -ne 0 ] || { [ $shead_pt != I ] || shead_cl="-c 6" ;}
shead_nr=$((shead_nr+1))
[ $shead_pt = P -o $shead_pt = S ] && {
[ ${#shead_cl} -eq 0 ] || tize -v shead_rp $shead_cl $shead_rp
# echo --$shead_rp--$tr--
[ $shead_pt = P ] && shead_tx=$shead_rp$tr || shead_tx=$tr$shead_rp
} || {
[ $shead_pt != I -a ${#shead_cl} -eq 0 ] && shead_ne="-n"
tize -v shead_tx -s $sp $shead_ne $shead_cl $shead_ft -r $shead_rp $sp "$shead_tx"
}
}
#aplicar
$shead_nm && ! $shead_mi && ln=$shead_ct"\t" || :
shead_rs="$shead_rs$ln$shead_tx$shead_sl"
}
}
shead(){
local dp="$PWD" ae="" cm="" as="" td="" it="" sp="/" in=1 ul=0 at=$tmp/shead
# dp directorio_previo - ae archivo - cm comodin - as archivo_salida - td todo - it instancia - sp separador - in inicial - ul ultima - at archivo_temporal
shead_nm=false shead_tx="" shead_ft="" shead_rs="" shead_pt="I" shead_md=false shead_ct=0 shead_sl='\n' shead_rp=""
shead_cr=false shead_nr=0 shead_mt=false shead_ne="" shead_mi=false shead_cl=""
# nm numerar - tx texto - ft filtro - rt resultado - pt parte - md modo - ct contador - sl salto_linea - rp reemplazo - dr devolver_resultados - nr nº_resultados - mt mostrar_todo - nm numerar_lineas - mi multiinstancias - cl color - ne sin estilo
[ $# -ne 0 ] || { infsh ~/code/shead 3 38 -c 7 filtro ¬ -c 5 Ultima opcion ; return ;}
# echo "pid_orden(\$$) $$ -- orden(\$0) $0 -- nº_param(\$#) $# -- param(\$@) $@"
# echo "ultimo_param(\$_) $_ -- pid_ultimo_proc(\$!) $! -- codigo_salida(\$?) $?"
while [ $# -ne 0 ] ; do case "$1" in # opciones generales
-a) path ae "$2" && shift 2 || { echo "No existe el archivo $2" ; return ;} ;;
-o) sa=true as="$2" ; shift 2 ;;
-l) shead_nm=true ; shift ;;
# -z) shead_sl="" ; shift ;; # movido a ops. instancias
-n) shead_ne="-n" ; shift ;;
-t) shead_mt=true ; shift ;;
-S) sp="$2" ; shift 2 ;;
-C) shead_cr=true shead_ne="-n" ; shift ;;
-f) [ ${#ae} -ne 0 ] && {
while read shead_tx ; do ul=$((ul+1)) ; done < "$ae" ; in=$((ul+1-$2))
} || {
>$at
while read shead_tx ; do ul=$((ul+1)) ; echo $shead_tx >> $at ; done ; in=$((ul+1-$2))
ae=$at
}
shift 2 ;;
*) [ ${#ae} -eq 0 -a -e "$1" ] && {
path ae "$1" ; shift
} || {
isnum $1 && {
in=$1 ; shift ; isnum $1 && { ul=$1 ; shift ; break ;}
} || break
} ;;
esac ; done
cd $tmp/vacio
td=$@
while : ; do #Gestion de instancias
it=${td%% $sp *} td=${td#$it $sp }
set -- $it
#opciones de instancia
shead_cl=""
while [ $# -ne 0 ] ; do case "$1" in
!|-N) shift; shead_md="" ;;
-|-q) shift; shead_md=true ;;
-p) shift; shead_pt="P" ;;
-pp) shift; shead_pt="PP" ;;
-s) shift; shead_pt="S" ;;
-ss) shift; shead_pt="SS" ;;
-c) shead_cl="-c $2" ; shift 2 ;; #color
-P) shead_cl="$shead_cl -p" ; shift ;; #parpadeo
-u) shead_cl="$shead_cl -u" ; shift ;; #subrayado
-b) shead_cl="$shead_cl -b" ; shift ;; #no bold
-z) shead_sl=" " ; shift ;;
--)shift;break;;
*)break;;
esac ; done
#filtro
shead_ft=$@
[ "$shead_ft" != "${shead_ft#* -r }" ] && { #si reemplazar
shead_rp=${shead_ft##* -r } shead_ft=${shead_ft%% -r *} #shead_rp=$shead_ft" -r "$shead_rp
} || { #si reemplazar por nada
[ "$shead_ft" = "${shead_ft#* -r}" ] || shead_ft=${shead_ft%% -r} shead_rp='\b' #shead_rp=$shead_ft" -r "'\b'
}
[ $ul -ne 0 ] || ul=10000
#Accion
[ ${#ae} -ne 0 ] && {
while read shead_tx ; do
shead_ct=$((shead_ct+1))
[ $shead_ct -ge $in ] || continue
shead_fun
shead_tx="" #(1)
[ $shead_ct -lt $ul ] || break
done < "$ae"
[ ! "$shead_tx" ] || shead_fun #(1)
} || {
while read shead_tx ; do
shead_ct=$((shead_ct+1))
[ $shead_ct -ge $in ] || continue
shead_fun
[ $shead_ct -lt $ul ] || break
done
}
[ "$td" != "$it" ] || break
ae=$at in=1 shead_mi=true shead_nr=0 shead_rp="" shead_pt="I" shead_md=false shead_ct=0 shead_ft=""
[ ${#BASH} -ne 0 ] && echo -e -n "$shead_rs" > "$ae" || echo -n "$shead_rs" > "$ae"
shead_rs=""
done
cd "$dp"
$shead_cr && return $shead_nr || { # nº de resultados
[ ${#as} -eq 0 ] && { # salida a pantalla
[ ${#BASH} -ne 0 ] && echo -e -n "$shead_rs" || echo -n "$shead_rs"
} || { # salida a archivo
[ ${#BASH} -ne 0 ] && echo -e -n "$shead_rs" >> "$as" || echo -n "$shead_rs" >> "$as"
}
}
}
# Evitar conflictos entre corchetes/comodines y nombres de archivo:
# ' [*nombre archivo monocaracter*] ' '?nam' 'nam?' '*nam' 'nam*'
# que provocan al ser enviados a una funcion o programa, que reciben los nombres
# de los archivos que hay en el directorio desde donde se lanzo la orden.
# SOLUCION ACTUAL
# Directorio de trabajo vacio
# mkdir -m -w -p $tmp/vacio
[ ${0##*/} != shead ] || shead "$@"
# GESTION MANUAL DE * ,reemplaza con ?[?...] - ya no hace falta
# . enum
# x=1234567
#prefijo
# y=*6?
# [ "${y#"*"}" = "$y" ] || { enum c $((${#x}-(${#y}-1))) ; y=${y#"*"} ; for i in $c ; do y='?'$y ; done ;}
# echo --"$y"--${x#$y}--
#sufijo
# y=?2*
# [ "${y%"*"}" = "$y" ] || { enum c $((${#x}-(${#y}-1))) ; y=${y%"*"} ; for i in $c ; do y=$y'?' ; done ;}
# echo --"$y"--${x%$y}--
#
#