Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Been banging my head off a wall on this

bash variable LATLNG contains a latitude & longitude value in brackets like so


I want to parse these into a variable called LAT and LON which I'm trying to do via sed like so

LAT=$(sed "s/(\(.*\),\(.*\))/\1/g" "$LATLNG")
LON=$(sed "s/(\(.*\),\(.*\))/\2/g" "$LATLNG")

However, I get the following error:

sed: can't read (53.3096,-6.28396): No such file or directory

I'm obviously doing something silly here but it's very late and I cant see what it is. I've tried several other variations without luck too.

share|improve this question
Learn a scripting language like Python, Ruby, maybe even PERL, so that you don't have to force the shell to do everything for you. You can even get Javascript (Rhino) to run on a UNIX system and almost everyone needs to know some Javascript. – Michael Dillon Feb 4 '11 at 3:17
up vote 15 down vote accepted

This can be solved via pure shell syntax. It does require a temp variable because of the parentheses (brackets) though:




Alternatively, you can do it in one go by playing with IFS and using the read builtin:



IFS='( ,)' read _ LAT LNG _ <<<"$LATLNG"
share|improve this answer
First example will work only in bash, not in POSIX shell. – gelraen Jan 29 '11 at 22:30
@gelraen hence the #!/bin/bash – SiegeX Jan 30 '11 at 4:51
See more expansions here: gnu.org/software/bash/manual/… – Ricardo Stuven Jun 11 at 20:05

SiegeX's answer is better for this particular case, but you should also know how to pass arbitrary text to sed.

sed is expecting filenames as its second, third, etc. parameters, and if it doesn't find any filenames, it reads from its standard input. So if you have text that you want to process that's not in a file, you have to pipe it to sed. The most straightforward way is this:

echo "blah blah" | sed 's/blah/blam/g'

So your example would become:

LAT=$(echo "$LATLNG" | sed 's/(\(.*\),\(.*\))/\1/g')
LON=$(echo "$LATLNG" | sed 's/(\(.*\),\(.*\))/\2/g')

Alternate (better but more obscure) Methods

If you think there's any chance that $LATLNG could begin with a dash, or if you want to be pedantic, you should use printf instead of echo:

printf '%s' "$LATLNG" | sed 's/foo/bar/g'

Or a "here document", but that can be a little awkward with the construct you're using:

LAT=$(sed 's/foo/bar/g' <<END

Or if you're using bash and not worried about portability, you can use a "here string":

sed 's/foo/bar/g' <<< "$LATLNG"
share|improve this answer

Here's a solution that will work in any POSIX shell:

parse_coordinates () {
  IFS='(), '  # Use these characters as word separators
  set -f      # Disable globbing
  set $1      # Split $1 into separate words
  set +f      # Restore shell state
  unset IFS
  LAT=$2      # $1 is the empty word before the open parenthesis
parse_coordinates "$LATLNG"

Here's another equally portable solution that parses the specific syntax used.

LAT=${LATLNG%\)}    # strip final parenthesis
LAT=${LAT#\(}       # strip initial parenthesis
LON=${LAT##*[, ]}   # set LON to everything after the last comma or space
LAT=${LAT%%[, ]*}   # set LAT to everything before the first comma or space
share|improve this answer
What does it matter if it matches a glob? Is the value of either variable not the same? And how can [0-9-.] glob anyway? – mikeserv Jun 19 '14 at 7:25
If you really want to though, you can avoid the glob anyway like: export $(IFS='(),' ; printf '%s LAT=%s LON=%s %s' ${0+(53.3096,-6.28396)}) ; echo $LAT $LON - that's done in a subshell so you could do your set -f if you want without resetting it, but it wouldn't matter because parameter expansion doesn't glob. – mikeserv Jun 19 '14 at 7:33
set ${LATLNG//[(,)]/ }
share|improve this answer
You should probably do set -- ... There is a very good chance the first character is a -. – mikeserv Jun 19 '14 at 7:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.