Search code examples
awksedservicegrep

how to obtain the service of a computer in a csv format


I want to get the service of a computer using :

systemctl list-unit-files --no-legend -t service | awk '{ sub(/\.service$/, "", $1); print "SERVICE:" $1 "|" $2 }'

but it gives me the Following result :

SERVICE: syslog|enable

(for example)

and i would love to get this :

"SERVICE","syslog","enable",

I tried several syntax but it generates errors because of the ' "" '. i even tried to put some other printf '","' but it brake the awk sentence because of the the ' at the beginning.

i tried to use sed and grep but it modifies the

systemctl list-unit-files --no-legend -t service

directly which breaks my script.

How can i parse the result in a CSV format ?

Thanks a lot for your help.


Solution

  • Suggesting to try this solution:

    systemctl list-unit-files --no-legend -t service| awk '{printf("\"SERVICE\",\"%s\",\"%s\"\n",$1,$2)}'