P
P
Porfo2020-12-17 12:42:20
bash
Porfo, 2020-12-17 12:42:20

How to delete by pattern?

There is a text like

d-auth-service-5f5f466864-bdskb
demodent-api-gateway-677bf48f98-gmn2n
demomed-client-widget-6c58686f69-qvj9g

It is necessary to remove postfixes of the format -xxxxxxxxxx-xxxxxx

i.e. to from demomed-analysis-service-6f955bff79-cqjv9was d emomed-analysis-service
Thank you

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FirststepsRu, 2020-12-17
@Porfo

Maybe someone will write more beautifully, it's interesting to see.

LIST="d-auth-service-5f5f466864-bdskb demodent-api-gateway-677bf48f98-gmn2n demomed-client-widget-6c58686f69-qvj9g"
for L in $LIST; do
  echo "src: $L"
  OIFS=$IFS; IFS="-"; N=($L); IFS=$OIFS; Z=${N[0]}
  for ((I=1; $I < `expr ${#N[*]} - 2`; I=`expr $I + 1`)); do
    Z=$Z-${N[$I]}
  done
  echo "result: $Z"
done

Result
src: d-auth-service-5f5f466864-bdskb
result: d-auth-service
src: demodent-api-gateway-677bf48f98-gmn2n
result: demodent-api-gateway
src: demomed-client-widget-6c58686f69-qvj9g
result: demomed-client-widget

After a while, another thought came to me:
for L in $LIST; do
      echo "src - $L"
      Z=`echo $L | cut -f1-3 -d'-'`
      echo "result - $Z"
done

X
xibir, 2020-12-17
@xibir

echo 'd-auth-service-5f5f466864-bdskb
demodent-api-gateway-677bf48f98-gmn2n
demomed-client-widget-6c58686f69-qvj9g' | sed -r 's/(.+)-{10}-{5}$/\1/'

S
Saboteur, 2020-12-17
@saboteur_kiev

sed -i 's/(.+)-\d{10}-\d{5}$/\1/g' filename

L
Lynn "Coffee Man", 2020-12-17
@Lynn

Well, since such a booze has gone

LIST="d-auth-service-5f5f466864-bdskb demodent-api-gateway-677bf48f98-gmn2n demomed-client-widget-6c58686f69-qvj9g"
for L in $LIST; do
  echo ${L%-*-*}
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question