Question:
Linux Shell Script - how to split string into variables?
1970-01-01 00:00:00 UTC
Linux Shell Script - how to split string into variables?
Six answers:
?
2016-11-13 06:30:58 UTC
Shell Script Split String
frasier
2016-10-21 03:18:18 UTC
char str[c0c7c76d30bd3dcaefc96f40275bdcac0c7c76d30bd3dcaefc96f40275bdca] = "right here,it,is"; char *pch; pch = strtok(str,","); //get token a million - (right here) mutually as(pch != NULL) { cout<
iammisc
2007-07-12 16:56:00 UTC
this would be way better suited to perl and its regular expressions.
LAMPP
2007-07-12 01:06:08 UTC
I'm not really sure what's your problem but I guess you need to assign a certain values (172.28.65.253) to a specific variable name (computer). If am correct then try these sets of commands:



declare computer=172.28.65.253 ## to assigned to a variable

echo $computer ## to check the value of a variable
2007-07-12 01:05:04 UTC
Lots of ways



with bash built ins see man bash

with awk print command

with grep and regular expressions

simple is cut

cut -f 3 -d " " | cut -f 0 ":", will give you 172.28.65.253



Yes cut based on the delimineter will always cut a space it doesn't matter what the ip is or how many bytes, If it always uses that format cut will work.
J-Dogg
2007-07-13 01:10:30 UTC
easy :)



Let's assume your variable for the ping response line is called $PINGRESPONSE



export PINGRESPONSE="64 bytes from 172.28.65.253: icmp_seq=1 ttl=128 time=0.221 ms"



this is how you'd get the info you want out of it using standard linux/unix commands:



COMPUTER=`echo $PINGRESPONSE | awk '{print $4}' | sed 's/\:$//'`



TIME=`echo $PINGRESPONSE | awk '{print $7}' | sed 's/time=//'`



-- the ` ` tell the variable to execute the command enclosed in ` ` quotes (the quote key next to the number "1")

-- the command basically will echo your ping response variable

-- that output is piped to the awk command which you're telling to split it up into logical chunks (awk uses a space as it's default field delimiter), {print $4} tells awk to print out the 4th chunk (172.28.65.253:) note the : colon in there

- the above output is then piped to sed, the 's/:$//'` part of it is basically a search & replace of the :$ ($ denotes "end of line") so basically it says "search for : at the end of 172.28.65.253 and replace ":" with "" or, nothing. this leaves you with 172.28.65.253 as your final output.

- since the command is finished, the variable uses "172.28.65.253" as it's value



the same applies to the time variable


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...