Home > Net >  How to I create two variables by splitting a string in bash/zsh?
How to I create two variables by splitting a string in bash/zsh?

Time:02-22

I'm trying to input something like "New York, USA" and get $city as "new-york" and $co as "usa"

cotime() {
  input=$(echo $1 | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
  arr=(${input//,/ })
  city=${arr[0]}
  co=${arr[1]}
  echo $city
  echo $co
  xidel -s "https://www.timeanddate.com/worldclock/${co}/${city}" -e '//*[@id="ct"]/text()'
}

I'm getting this output:

$ cotime 'new york,usa'

new-york usa

which isn't quite correct...

CodePudding user response:

One way to do it is to use the good old "cut":

input='new-york,usa'
city=$(echo $input |cut -d, -f1)
co=$(echo $input |cut -d, -f2)
echo $city, $co
new-york, usa

CodePudding user response:

I suggest it would be simpler to construct your URL directly with AWK using the comma as seperator.

theURL=$(echo $1 | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | awk -F',' '{ print "https://www.timeanddate.com/worldclock/" $2 "/" $1 }')
echo $theURL
xidel -s $theURL

This gives the following URL which works.

https://www.timeanddate.com/worldclock/usa/new-york

  • Related