JonesCity
JonesCity

Reputation: 25

How to remove special characters/escape squences using erlang os:cmd()?

Lately I've been using os:cmd to run commands on my local computer. When I use os:cmd. Not only do I get the output of my said command but the resulting strings and output produces or "inserts" white spaces and escapes sequences in my output/string example:

os:cmd("WMIC BIOS GET SERIALNUMBER").

Produces:

"SerialNumber \r\r\nCN9B4R1 \r\r\n\r\r\n"

I tried searching for the erlang docs but nothing in the IO lib reaches out at me or ring a bell.

Thank you guys so much!

Upvotes: 2

Views: 497

Answers (1)

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26121

What you got is the complete output of the command. os:cmd/1 doesn't include anything. You have to process output and remove unwanted characters as you would in any other language as well because what you got is exactly what the command produces. Use io_lib, string, lists or re module for parsing of the output. For example using string:tokens\2 you can check and obtain serial number like

Output = os:cmd("WMIC BIOS GET SERIALNUMBER"),
["SerialNumber", SerialNumber] = string:tokens(Output, " \r\n").

Upvotes: 4

Related Questions