Quiero imprimir mis twigs de git usando awk, y solo quiero el nombre de la twig, sin el origen y sin saber cuál es mi copy de trabajo. Vine con la git branch -r | awk -F' ' '{print $1}'
git branch -r | awk -F' ' '{print $1}'
pero el resultado es el siguiente:
origin/Dev origin/HEAD origin/master
Así que probé git branch -r | awk -F'/' '{print $2}'
git branch -r | awk -F'/' '{print $2}'
y el resultado fue:
Dev HEAD -> origin master
¿Cómo puedo replace en la misma línea la línea se rompe con la coma? Por ejemplo, mi objective es verlo así:
Dev,HEAD,master
Gracias.
Esto imprimirá el resultado deseado, la secuencia separada por comas
git branch -r | awk ' BEGIN { firstBranch = 1 } # split branch off from remote { branch = substr($1, index($1, "/") + 1) } # eliminate HEAD reference branch == "HEAD" { next } # output comma between branches !firstBranch { printf "," } firstBranch { firstBranch = 0 } # output branch name { printf branch } # final linebreak END { print "" } '
o como una línea sin comentarios
git branch -r | awk 'BEGIN { firstBranch = 1 } { branch = substr($1, index($1, "/") + 1) } branch == "HEAD" { next } !firstBranch { printf "," } firstBranch { firstBranch = 0 } { printf branch } END { print "" }'
... | awk -F' +|/' -v ORS=, '{if($3!="HEAD") print $3}'
Esto debería hacerlo:
awk -v OFS="," '{ n = split($1, a, /\//); branches[++b] = a[n] } END { for (i = 1; i <= b; ++i) printf "%s%s", branches[i], (i<b?OFS:ORS) }'
Esto obtiene la última parte de cada twig local (se supone que sus sucursales locales no tienen /
en sus nombres). Almacena cada nombre en una matriz y luego los imprime, separados por una coma (usando OFS
) y seguidos por una nueva línea (usando ORS
).
Sería fácil agregar un condicional if (a[n] != "HEAD")
antes de agregar la twig a la matriz.
Aquí mi ejemplo:
git branch|awk '{print ($1=="*" ? $2 : $1)}'|awk 'ORS=NR?",":"\n"'
git branch
– imprime twigs,
awk '{print ($1=="*" ? $2 : $1)}'
– imprime el nombre, no *
antes de la sucursal actual,
awk 'ORS=NR?",":"\n"'
– reemplace \n
con \n
Mi resultado es:
ZIIPR-1247,ZIIPR-Config,develop,master,stage
Espero que sea el ejemplo más simple …
git branch -r | awk -F/ '{sub(/[[:space:]]*->.*$/,"")}{print $NF}'
Deberías hacerlo.
Salida
Dev HEAD master