linux - Extract part of string using shell script -
strings:
"v1.0.20170728180812 latest ef1fc15" "latest ef1fc15 v1.0.20170728180812-test" "ef1fc15 v1.0.20170722150727 latest" expected string result:
"v1.0.20170728180812" "v1.0.20170728180812-test" "v1.0.20170722150727" code:
string='latest ef1fc15 v1.0.20170728180812-test' replace=$(echo $string | sed 's/v.*$//g') echo $string | sed -e 's/$(echo $replace)//g' thanks guys! ;)
this script used valid version of aws ecr, , deploy kubernetes --apply (avoiding caching when used "latest").
complete code:
ecrversions=$(aws ecr describe-images --repository-name <ecr_repo_name> --image-ids imagetag=latest --output text) appversion=$(echo $ecrversions | awk '{for (i=1;i<=nf;++i) {if($i~/^[v]/){print $i}}}') echo $appversion
using awk:
awk '{for (i=1;i<=nf;++i) {if($i~/^[v]/){print $i}}}' yourfile.txt this is:
- iterating file record record (because that's awk does)
- iterating each field delimited default :
for (i=1;i<=nf;++i) - testing see if field starts "v":
if($i~/^[v]/) - if prints field value
print $i
i'm sed make quick work of too.
Comments
Post a Comment