3

Yo tengo esta sentencia en el archivo xxx.txt:

select camp1,camp2,campN from pricing_user.prc_pvp a join ( select werks, matnr, pvp_category_type, max(start_date) as recent_date from pricing_user.prc_pvp where werks = "0006" and pvp_category_type in (1,3) group by 1,2,3 ) b on a.matnr = b.matnr and a.werks = b.werks and a.pvp_category_type=b.pvp_category_type where recent_date = start_date

Si quiero saber cuántos registros me va a dar esa consulta, solo hago:

select count(*) from pricing_user.prc_pvp a join ( select werks, matnr, pvp_category_type, max(start_date) as recent_date from pricing_user.prc_pvp where werks = "0006" and pvp_category_type in (1,3) group by 1,2,3 ) b on a.matnr = b.matnr and a.werks = b.werks and a.pvp_category_type=b.pvp_category_type where recent_date = start_date

Este tipo de tarea lo hago muy repetitivo y quiero automatizarlo, pero para eso, tendría que cambiar todos los campos camp1, camp2,....campN por count(*).

El problema es que no lo logro hacer . Estoy haciendo esto:

sed -i 's/select/select count\(\*\);/g' xxx.txt|sed 's/from/;from/g' |awk -F";" '{print $1 " " $3}'

Pero la salida es:

select count(*) from pricing_user.prc_pvp a join ( select count(*)

y lo que quiero es:

select count(*) from pricing_user.prc_pvp a join ( select werks, matnr, pvp_category_type, max(start_date) as recent_date from pricing_user.prc_pvp where werks = "0006" and pvp_category_type in (1,3) group by 1,2,3 ) b on a.matnr = b.matnr and a.werks = b.werks and a.pvp_category_type=b.pvp_category_type where recent_date = start_date

1 respuesta 1

3

Ya vi cómo resolverlo: hago que sed no sea global

sed -i 's/select/select count\(\*\);/' xxx.txt|sed 's/from/;from/' |awk -F";" '{print $1 " " $3}'

Es decir, en lugar de decir sed 's/busco/sustituyo/g', hago sed 's/busco/sustituyo/ (sin la g final) para que haga la sustitución una única vez.

$ echo "2 3 2 4" | sed 's/2/XX/'
XX 3 2 4  # el "2" se ha sustituido una única vez

$ echo "2 3 2 4" | sed 's/2/XX/g'
#                              ^
XX 3 XX 4  # el "2" se ha sustituido múltiples veces

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.