Discard any characters that are not actual letters. using awk - Bash

Viewed 168

So I have a script that seeks whether my service vmxd.service exists, and checks its current state loaded,running,active...

I use the following script for that case.

systemctl --all --type service | awk -v pat="$SERVICE" '$0 ~ pat {print $0}'

● vmxd.service                                          loaded    failed   failed  Juniper vMX Router

Bear in mind that $SERVICE="vmxd.service"

The problem is that in some servers, that [●] is not present, so I deducted that by printing the first word {print $1} it would make the trick of grabbing the name.

When I checked my script on another host, now instead of printing vmxd.service as it was the first character on the string, it is now printing [●] and totally breaks my script.

Here is an Output of my Script showing the [●]
[...] 
The service file: vmxd.service has been found under /etc/systemd/system/
Check if WorkingDirectory path is correct............................[OK]
Check if service exist...............................................[Found]
Check if ● is loaded...............................................[No]
Check if ● is active...............................................[No]
Enabling service....................................................../init.sh: line 189: command not found
Reloading Service....................................................[No]
Check if 'vmxd.service' has started successfully.....................[Started]

Is there a workaround for this issue? Is there a way to, if [●] is detected ignore without altering the {print $1}. By printing $1 it should say vmxd.service regardless

Normally the output on the servers where my script works are like:

  vmxd.service                                          loaded    active   exited  Juniper vMX Router

without the [●].

Note: I have no much idea what that dot actually means, but regardless I just need to ignore that character from my variable running the command

2 Answers

You can remove all non-ascii characters from awk output:

systemctl --all --type service |
awk -v pat="$SERVICE" '$0 ~ pat {sub(/[^\x01-\x7f]+/, ""); print}'

Change from your script is this call:

sub(/[^\x01-\x7f]+/, "")

Regex pattern [^\x01-\x7f]+ matches 1+ of any character that is NOT in the ASCII range of x01-x7F (1-127) and removes them by replacing it with an empty string.

Is there a workaround for this issue? Is there a way to, if [●] is detected ignore without altering the {print $1}. By printing $1 it should say vmxd.service regardless

You might inform GNU AWK to treat only letters digits and dots using FPAT variable consider following simple example, let file.txt content be

● vmxd.service                                          loaded    failed   failed  Juniper vMX Router
vmxd.service                                          loaded    active   exited  Juniper vMX Router

then

awk 'BEGIN{FPAT="[0-9A-Za-z.]+"}{print $1}' file.txt

output

vmxd.service
vmxd.service

(tested in GNU Awk 5.0.1)

Related