Processing bash variable with sed
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
bash variable LATLNG contains a latitude & longitude value in brackets like so
(53.3096,-6.28396)
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
bash shell scripting shell-script sed
add a comment |
bash variable LATLNG contains a latitude & longitude value in brackets like so
(53.3096,-6.28396)
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
bash shell scripting shell-script sed
1
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
From where does these values come? It would be more efficient to parse the latitude and longitude values out from the original source than adding an extra step by putting them in abash
variable.
– Kusalananda♦
Mar 18 at 9:40
add a comment |
bash variable LATLNG contains a latitude & longitude value in brackets like so
(53.3096,-6.28396)
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
bash shell scripting shell-script sed
bash variable LATLNG contains a latitude & longitude value in brackets like so
(53.3096,-6.28396)
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
bash shell scripting shell-script sed
bash shell scripting shell-script sed
edited Mar 18 at 4:04
Rui F Ribeiro
42.1k1484142
42.1k1484142
asked Jan 29 '11 at 6:51
conorgriffinconorgriffin
68851120
68851120
1
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
From where does these values come? It would be more efficient to parse the latitude and longitude values out from the original source than adding an extra step by putting them in abash
variable.
– Kusalananda♦
Mar 18 at 9:40
add a comment |
1
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
From where does these values come? It would be more efficient to parse the latitude and longitude values out from the original source than adding an extra step by putting them in abash
variable.
– Kusalananda♦
Mar 18 at 9:40
1
1
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
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
From where does these values come? It would be more efficient to parse the latitude and longitude values out from the original source than adding an extra step by putting them in a
bash
variable.– Kusalananda♦
Mar 18 at 9:40
From where does these values come? It would be more efficient to parse the latitude and longitude values out from the original source than adding an extra step by putting them in a
bash
variable.– Kusalananda♦
Mar 18 at 9:40
add a comment |
4 Answers
4
active
oldest
votes
This can be solved via pure shell syntax. It does require a temp variable because of the parentheses (brackets) though:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
tmp=$LATLNG//[()]/
LAT=$tmp%,*
LNG=$tmp#*,
Alternatively, you can do it in one go by playing with IFS
and using the read
builtin:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
IFS='( ,)' read _ LAT LNG _ <<<"$LATLNG"
First example will work only in bash, not in POSIX shell.
– gelraen
Jan 29 '11 at 22:30
1
@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 '16 at 20:05
add a comment |
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
$LATLNG
END
)
Or if you're using bash
and not worried about portability, you can use a "here string":
sed 's/foo/bar/g' <<< "$LATLNG"
add a comment |
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
LON=$3
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
add a comment |
LATLNG="(53.3096,-6.28396)"
set $LATLNG//[(,)]/
LAT=$1
LON=$2
You should probably doset -- ...
There is a very good chance the first character is a-
.
– mikeserv
Jun 19 '14 at 7:23
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f6629%2fprocessing-bash-variable-with-sed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can be solved via pure shell syntax. It does require a temp variable because of the parentheses (brackets) though:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
tmp=$LATLNG//[()]/
LAT=$tmp%,*
LNG=$tmp#*,
Alternatively, you can do it in one go by playing with IFS
and using the read
builtin:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
IFS='( ,)' read _ LAT LNG _ <<<"$LATLNG"
First example will work only in bash, not in POSIX shell.
– gelraen
Jan 29 '11 at 22:30
1
@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 '16 at 20:05
add a comment |
This can be solved via pure shell syntax. It does require a temp variable because of the parentheses (brackets) though:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
tmp=$LATLNG//[()]/
LAT=$tmp%,*
LNG=$tmp#*,
Alternatively, you can do it in one go by playing with IFS
and using the read
builtin:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
IFS='( ,)' read _ LAT LNG _ <<<"$LATLNG"
First example will work only in bash, not in POSIX shell.
– gelraen
Jan 29 '11 at 22:30
1
@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 '16 at 20:05
add a comment |
This can be solved via pure shell syntax. It does require a temp variable because of the parentheses (brackets) though:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
tmp=$LATLNG//[()]/
LAT=$tmp%,*
LNG=$tmp#*,
Alternatively, you can do it in one go by playing with IFS
and using the read
builtin:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
IFS='( ,)' read _ LAT LNG _ <<<"$LATLNG"
This can be solved via pure shell syntax. It does require a temp variable because of the parentheses (brackets) though:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
tmp=$LATLNG//[()]/
LAT=$tmp%,*
LNG=$tmp#*,
Alternatively, you can do it in one go by playing with IFS
and using the read
builtin:
#!/bin/bash
LATLNG="(53.3096,-6.28396)"
IFS='( ,)' read _ LAT LNG _ <<<"$LATLNG"
edited Jan 29 '11 at 7:11
answered Jan 29 '11 at 6:56
SiegeXSiegeX
5,57112723
5,57112723
First example will work only in bash, not in POSIX shell.
– gelraen
Jan 29 '11 at 22:30
1
@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 '16 at 20:05
add a comment |
First example will work only in bash, not in POSIX shell.
– gelraen
Jan 29 '11 at 22:30
1
@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 '16 at 20:05
First example will work only in bash, not in POSIX shell.
– gelraen
Jan 29 '11 at 22:30
First example will work only in bash, not in POSIX shell.
– gelraen
Jan 29 '11 at 22:30
1
1
@gelraen hence the
#!/bin/bash
– SiegeX
Jan 30 '11 at 4:51
@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 '16 at 20:05
See more expansions here: gnu.org/software/bash/manual/…
– Ricardo Stuven
Jun 11 '16 at 20:05
add a comment |
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
$LATLNG
END
)
Or if you're using bash
and not worried about portability, you can use a "here string":
sed 's/foo/bar/g' <<< "$LATLNG"
add a comment |
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
$LATLNG
END
)
Or if you're using bash
and not worried about portability, you can use a "here string":
sed 's/foo/bar/g' <<< "$LATLNG"
add a comment |
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
$LATLNG
END
)
Or if you're using bash
and not worried about portability, you can use a "here string":
sed 's/foo/bar/g' <<< "$LATLNG"
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
$LATLNG
END
)
Or if you're using bash
and not worried about portability, you can use a "here string":
sed 's/foo/bar/g' <<< "$LATLNG"
answered Jan 29 '11 at 8:00
JanderJander
11.9k43358
11.9k43358
add a comment |
add a comment |
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
LON=$3
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
add a comment |
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
LON=$3
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
add a comment |
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
LON=$3
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
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
LON=$3
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
edited Dec 10 '16 at 14:19
mikeserv
46.1k669164
46.1k669164
answered Jan 29 '11 at 12:31
GillesGilles
548k13011131631
548k13011131631
add a comment |
add a comment |
LATLNG="(53.3096,-6.28396)"
set $LATLNG//[(,)]/
LAT=$1
LON=$2
You should probably doset -- ...
There is a very good chance the first character is a-
.
– mikeserv
Jun 19 '14 at 7:23
add a comment |
LATLNG="(53.3096,-6.28396)"
set $LATLNG//[(,)]/
LAT=$1
LON=$2
You should probably doset -- ...
There is a very good chance the first character is a-
.
– mikeserv
Jun 19 '14 at 7:23
add a comment |
LATLNG="(53.3096,-6.28396)"
set $LATLNG//[(,)]/
LAT=$1
LON=$2
LATLNG="(53.3096,-6.28396)"
set $LATLNG//[(,)]/
LAT=$1
LON=$2
edited Jun 19 '14 at 8:51
jasonwryan
50.9k14135190
50.9k14135190
answered Jun 19 '14 at 7:08
2nil2nil
1
1
You should probably doset -- ...
There is a very good chance the first character is a-
.
– mikeserv
Jun 19 '14 at 7:23
add a comment |
You should probably doset -- ...
There is a very good chance the first character is a-
.
– mikeserv
Jun 19 '14 at 7:23
You should probably do
set -- ...
There is a very good chance the first character is a -
.– mikeserv
Jun 19 '14 at 7:23
You should probably do
set -- ...
There is a very good chance the first character is a -
.– mikeserv
Jun 19 '14 at 7:23
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f6629%2fprocessing-bash-variable-with-sed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
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
From where does these values come? It would be more efficient to parse the latitude and longitude values out from the original source than adding an extra step by putting them in a
bash
variable.– Kusalananda♦
Mar 18 at 9:40