# [$DEVICE] Network started
# Packet data handle: '12345678'
# [$DEVICE] Client ID not released:
# Service: 'wds'
# CID: '12'
What I need is the CID number, just the number, and store that in a variable or a file so I can call that when another command is later given to my script, like “stop” or “restart” for example.
I have no idea how to do that and if that is even possible in bash scripting, I know I probably have to regex it out of there but does bash (awk, sed and so on) allow only selecting a group of a regex like python does?
I only ever did some basic python regex so I’m very new to bash regex and variables and such.
I know of pipes and “>” and so on to write to file but I don’t know how that handle this.
Could someone tell me what to search for or help me out here otherwise please?
grep CID will filter your modem output for just the line that contains the CID number. The awk '{print $3}' command will print the third field as it’s delimited by spaces and will output ‘12’ with the quotes. Finally, the tr -d "'" trims the quotes off the front and back of the string, leaving you with your number that you can redirect into your pidfile.
Hey thank you very much
With that I can go further
Sry I failed you in not removing the “#” so it needs to be “… awk ‘{print $2}’ …”
I so forgot about “grep” that it hurts
Yo! This will be my golden chance to be “that guy” that crashes question threads telling you can replace all those pipes with a monstrous single unintelligible awk command!
awk is one of those commands that I have somehow avoided for a long time. It’s been on my todo list to check out and get some basic proficiency with. I’ll probably kick myself afterwards for avoiding it for so long.
I actually originally tried to use cut when I wrote my original solution above, with something along these lines: cut -d ' ' -f 3 to set the delimiter (-d) to the space character and then get the third field (-f). The problem is that cut doesn’t group the separators together so you’ll get a bunch of empty fields when you have multiple separators like OP had originally. Is there a way cut could work here? I’ve found it much more useful when parsing fixed-width fields and things like that in the past.
Well its not like the previous solution didn’t worked. I’m being that guy precisely because the awk parse is way more confusing and seemingly ugly than the one using lots of pipes. It was meant to be a joke please don’t take it seriously.
Stop already- you’re casually throwing around all the linux commands that are black box mysteries to me. It’s like there is secret /usr/bin folder out there somewhere that I haven’t found.
1- search string
2- replace a single quote with nothing. "'" -> "". It is heavily escaped because ’ is an important operator for awk.
3- print third word in the string for each line processed