I want to clean my file before/after saving so I have to delete unnecessary characters that I have there. Sadly, even that my regex is working in Regex101, it does not work in shell script I wrote.
I am getting my list from Kubernetes via
kubectl get pods -n $1 -o jsonpath='{range .items[*]}{@.spec.containers[*].image}{","}{@.status.containerStatuses[*].imageID}{"\n"}{end}'
Then I saving it to the temp
file and using sed
to clear it - the regex should match and (sed should) delete any character between ,
and @
(also should delete @). I am escaping them since they are special characters.
sed -i 's/(?<=\,)(.*?)(?<=\@)//g' temp
The problem is that this regex is working fine (for example in Regex101) but is not working with the sed
command. I even tried awk
but getting the same output.
awk '!/(?<=\,)(.*?)(?<=\@)/' temp
Am I missing something or is the regex acting differently somehow in Unix/shell?
Thanks for any input.
Example content of the file (for test):
docker.elastic.co/elasticsearch/elasticsearch:7.17.5,docker-pullable://docker.elastic.co/elasticsearch/elasticsearch@sha256:76344d5f89b13147743db0487eb76b03a7f9f0cd55abe8ab887069711f2ee27d
docker.io/bitnami/kafka:3.3.1-debian-11-r11,docker-pullable://bitnami/kafka@sha256:be29db0e37b6ab13df5fc14988a4aa64ee772c7f28b4b57898015cf7435ff662
docker.io/bitnami/mongodb:6.0.3-debian-11-r0,docker-pullable://bitnami/mongodb@sha256:e7438d7964481c0bcfcc8f31bca2d73022c0b7ba883143091a71ae01be6d9edb
docker.io/bitnami/postgresql:14.1.0-debian-10-r80,docker-pullable://bitnami/postgresql@sha256:6eb9c4ab3444e395df159e2cad21f283e4bf30802958467590c886f376dc9959
docker.io/bitnami/zookeeper:3.8.0-debian-11-r47,docker-pullable://bitnami/zookeeper@sha256:0f3169499c5ee02386c3cb262b2a0d3728998d9f0a94130a8161e389f61d1462
Expected output:
docker.elastic.co/elasticsearch/elasticsearch:7.17.5,sha256:76344d5f89b13147743db0487eb76b03a7f9f0cd55abe8ab887069711f2ee27d
docker.io/bitnami/kafka:3.3.1-debian-11-r11,sha256:be29db0e37b6ab13df5fc14988a4aa64ee772c7f28b4b57898015cf7435ff662
docker.io/bitnami/mongodb:6.0.3-debian-11-r0,sha256:e7438d7964481c0bcfcc8f31bca2d73022c0b7ba883143091a71ae01be6d9edb
docker.io/bitnami/postgresql:14.1.0-debian-10-r80,sha256:6eb9c4ab3444e395df159e2cad21f283e4bf30802958467590c886f376dc9959
docker.io/bitnami/zookeeper:3.8.0-debian-11-r47,sha256:0f3169499c5ee02386c3cb262b2a0d3728998d9f0a94130a8161e389f61d1462
CodePudding user response:
You are trying to use Perl extensions which are not supported by more traditional regex tools like sed
and Awk.
Perhaps see also Why are there so many different regular expression dialects? and the Stack Overflow regex
tag info page.
If I can guess what you are trying to do, you want simply
sed -i 's/,[^@]*@/,/g' temp
The /g
flag is unnecessary if you only expect one match per line.
Neither ,
nor @
is a regex metacharacter; they do not require escaping.
Usually you would want to avoid using a temporary file or sed -i
; perhaps simply
kubectl blah blah | sed 's/,[^@]*@/,/' > temp
to create the file, or remove the redirection if you want to pipe the results further.