Is the command tr capable of translating characters into longer strings?
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
tr
is a great tool that can be used to translate set of characters
into another set of characters
.
Commands such as sed , awk, perl
can be used to translate set of characters
into another set of strings, for example, it can translate <
and >
into <
and >
, but it seems unnecessary to use these commands for that purpose!
Is there any other command(s) that can accomplish this?
shell string tr
add a comment |Â
up vote
7
down vote
favorite
tr
is a great tool that can be used to translate set of characters
into another set of characters
.
Commands such as sed , awk, perl
can be used to translate set of characters
into another set of strings, for example, it can translate <
and >
into <
and >
, but it seems unnecessary to use these commands for that purpose!
Is there any other command(s) that can accomplish this?
shell string tr
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
tr
is a great tool that can be used to translate set of characters
into another set of characters
.
Commands such as sed , awk, perl
can be used to translate set of characters
into another set of strings, for example, it can translate <
and >
into <
and >
, but it seems unnecessary to use these commands for that purpose!
Is there any other command(s) that can accomplish this?
shell string tr
tr
is a great tool that can be used to translate set of characters
into another set of characters
.
Commands such as sed , awk, perl
can be used to translate set of characters
into another set of strings, for example, it can translate <
and >
into <
and >
, but it seems unnecessary to use these commands for that purpose!
Is there any other command(s) that can accomplish this?
shell string tr
shell string tr
edited Sep 3 at 14:20
Goro
4,68452357
4,68452357
asked Jul 8 '11 at 14:13
user4518
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
down vote
I don't know which shell you are using, but chances are tr
is not a builtin. It's sure not for me in bash or zsh :) Try running type tr
to see for yourself.
In all likelyhood, sed
is the right tool for the job. It's what it was made for. As appropriate perl
and awk
too.
If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.
string="Stuf with <tag> in it."
escaped=$string//</<
escaped=$escaped//>/>
echo $escaped
I've fixed the terminology :)
â user4518
Jul 8 '11 at 14:24
add a comment |Â
up vote
4
down vote
Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by --
if ambiguous). How would you construct the synopsis in this case?
Here's a simple function which should do the job for simple replacements:
$ trs()
local string=$1
shift
for replacement in "$@"
do
string="$(sed -e "s/$replacement/g" <<< "$string")"
done
printf "$string"
$ trs '1 2 3' '1/foo' '2 3/bar baz'
foo bar baz
Alternatively you could work with pairs of parameters:
$ trs()
local string=$1
shift
while [ $# -gt 0 ]
do
string="$(sed -e "s/$1/$2/g" <<< "$string")"
shift 2
done
printf "$string"
$ trs '1 2 3' 1 foo '2 3' 'bar baz'
foo bar baz
These are nice functions. I hoped to find one that is readily available on a foreign system.
â user4518
Jul 8 '11 at 14:48
By the way, these wouldn't handle strings containing special strings like?
or&
.
â user4518
Jul 8 '11 at 14:54
1
@Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
â l0b0
Jul 8 '11 at 15:20
add a comment |Â
up vote
3
down vote
sed
can be scary, but simple character to string substitution with sed
isn't really too bad.
To substitute x
with the string bar
, do:
something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'
if it's a file you're wanting to process, you can use:
sed -e 's/x/bar/g' filename
(The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)
1
I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. notsed
ding when you cancut
.
â user4518
Jul 8 '11 at 14:44
2
how about notcat
ing when you can just usesed
â xenoterracide
Jul 8 '11 at 15:44
1
@xenoterracide: Good point, edited answer.
â LawrenceC
Jul 8 '11 at 16:02
The only thing that I don't like here is that becomes quite difficult to replace a newlinen
with a string like ` ; ` usingsed
: sed: How can I replace a newline (n)? - Stack Overflow notes that plains///
doesn't work, and you have to go into a "pattern space" syntax...
â sdaau
Jan 24 '15 at 3:12
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
I don't know which shell you are using, but chances are tr
is not a builtin. It's sure not for me in bash or zsh :) Try running type tr
to see for yourself.
In all likelyhood, sed
is the right tool for the job. It's what it was made for. As appropriate perl
and awk
too.
If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.
string="Stuf with <tag> in it."
escaped=$string//</<
escaped=$escaped//>/>
echo $escaped
I've fixed the terminology :)
â user4518
Jul 8 '11 at 14:24
add a comment |Â
up vote
5
down vote
I don't know which shell you are using, but chances are tr
is not a builtin. It's sure not for me in bash or zsh :) Try running type tr
to see for yourself.
In all likelyhood, sed
is the right tool for the job. It's what it was made for. As appropriate perl
and awk
too.
If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.
string="Stuf with <tag> in it."
escaped=$string//</<
escaped=$escaped//>/>
echo $escaped
I've fixed the terminology :)
â user4518
Jul 8 '11 at 14:24
add a comment |Â
up vote
5
down vote
up vote
5
down vote
I don't know which shell you are using, but chances are tr
is not a builtin. It's sure not for me in bash or zsh :) Try running type tr
to see for yourself.
In all likelyhood, sed
is the right tool for the job. It's what it was made for. As appropriate perl
and awk
too.
If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.
string="Stuf with <tag> in it."
escaped=$string//</<
escaped=$escaped//>/>
echo $escaped
I don't know which shell you are using, but chances are tr
is not a builtin. It's sure not for me in bash or zsh :) Try running type tr
to see for yourself.
In all likelyhood, sed
is the right tool for the job. It's what it was made for. As appropriate perl
and awk
too.
If you have very simple cases and if you are working with a string variable instead of a pipeline, you might get away with a real builtin such as using bash's string search and replace on a variable.
string="Stuf with <tag> in it."
escaped=$string//</<
escaped=$escaped//>/>
echo $escaped
edited Jul 8 '11 at 14:33
answered Jul 8 '11 at 14:20
Caleb
49.2k9146185
49.2k9146185
I've fixed the terminology :)
â user4518
Jul 8 '11 at 14:24
add a comment |Â
I've fixed the terminology :)
â user4518
Jul 8 '11 at 14:24
I've fixed the terminology :)
â user4518
Jul 8 '11 at 14:24
I've fixed the terminology :)
â user4518
Jul 8 '11 at 14:24
add a comment |Â
up vote
4
down vote
Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by --
if ambiguous). How would you construct the synopsis in this case?
Here's a simple function which should do the job for simple replacements:
$ trs()
local string=$1
shift
for replacement in "$@"
do
string="$(sed -e "s/$replacement/g" <<< "$string")"
done
printf "$string"
$ trs '1 2 3' '1/foo' '2 3/bar baz'
foo bar baz
Alternatively you could work with pairs of parameters:
$ trs()
local string=$1
shift
while [ $# -gt 0 ]
do
string="$(sed -e "s/$1/$2/g" <<< "$string")"
shift 2
done
printf "$string"
$ trs '1 2 3' 1 foo '2 3' 'bar baz'
foo bar baz
These are nice functions. I hoped to find one that is readily available on a foreign system.
â user4518
Jul 8 '11 at 14:48
By the way, these wouldn't handle strings containing special strings like?
or&
.
â user4518
Jul 8 '11 at 14:54
1
@Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
â l0b0
Jul 8 '11 at 15:20
add a comment |Â
up vote
4
down vote
Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by --
if ambiguous). How would you construct the synopsis in this case?
Here's a simple function which should do the job for simple replacements:
$ trs()
local string=$1
shift
for replacement in "$@"
do
string="$(sed -e "s/$replacement/g" <<< "$string")"
done
printf "$string"
$ trs '1 2 3' '1/foo' '2 3/bar baz'
foo bar baz
Alternatively you could work with pairs of parameters:
$ trs()
local string=$1
shift
while [ $# -gt 0 ]
do
string="$(sed -e "s/$1/$2/g" <<< "$string")"
shift 2
done
printf "$string"
$ trs '1 2 3' 1 foo '2 3' 'bar baz'
foo bar baz
These are nice functions. I hoped to find one that is readily available on a foreign system.
â user4518
Jul 8 '11 at 14:48
By the way, these wouldn't handle strings containing special strings like?
or&
.
â user4518
Jul 8 '11 at 14:54
1
@Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
â l0b0
Jul 8 '11 at 15:20
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by --
if ambiguous). How would you construct the synopsis in this case?
Here's a simple function which should do the job for simple replacements:
$ trs()
local string=$1
shift
for replacement in "$@"
do
string="$(sed -e "s/$replacement/g" <<< "$string")"
done
printf "$string"
$ trs '1 2 3' '1/foo' '2 3/bar baz'
foo bar baz
Alternatively you could work with pairs of parameters:
$ trs()
local string=$1
shift
while [ $# -gt 0 ]
do
string="$(sed -e "s/$1/$2/g" <<< "$string")"
shift 2
done
printf "$string"
$ trs '1 2 3' 1 foo '2 3' 'bar baz'
foo bar baz
Replacing multiple strings with another set of multiple strings becomes messy if you want to use standard parameter handling. Usually, commands take a set of atomic parameters, and then a set of atomic arguments, and the sequence doesn't matter other than that arguments come after parameters (separated by --
if ambiguous). How would you construct the synopsis in this case?
Here's a simple function which should do the job for simple replacements:
$ trs()
local string=$1
shift
for replacement in "$@"
do
string="$(sed -e "s/$replacement/g" <<< "$string")"
done
printf "$string"
$ trs '1 2 3' '1/foo' '2 3/bar baz'
foo bar baz
Alternatively you could work with pairs of parameters:
$ trs()
local string=$1
shift
while [ $# -gt 0 ]
do
string="$(sed -e "s/$1/$2/g" <<< "$string")"
shift 2
done
printf "$string"
$ trs '1 2 3' 1 foo '2 3' 'bar baz'
foo bar baz
answered Jul 8 '11 at 14:41
l0b0
26.4k17106232
26.4k17106232
These are nice functions. I hoped to find one that is readily available on a foreign system.
â user4518
Jul 8 '11 at 14:48
By the way, these wouldn't handle strings containing special strings like?
or&
.
â user4518
Jul 8 '11 at 14:54
1
@Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
â l0b0
Jul 8 '11 at 15:20
add a comment |Â
These are nice functions. I hoped to find one that is readily available on a foreign system.
â user4518
Jul 8 '11 at 14:48
By the way, these wouldn't handle strings containing special strings like?
or&
.
â user4518
Jul 8 '11 at 14:54
1
@Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
â l0b0
Jul 8 '11 at 15:20
These are nice functions. I hoped to find one that is readily available on a foreign system.
â user4518
Jul 8 '11 at 14:48
These are nice functions. I hoped to find one that is readily available on a foreign system.
â user4518
Jul 8 '11 at 14:48
By the way, these wouldn't handle strings containing special strings like
?
or &
.â user4518
Jul 8 '11 at 14:54
By the way, these wouldn't handle strings containing special strings like
?
or &
.â user4518
Jul 8 '11 at 14:54
1
1
@Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
â l0b0
Jul 8 '11 at 15:20
@Tim Nordenfur: If you only want to replace literals, you should escape it, yes. This approach allows for regular expressions, which of course is another can of worms.
â l0b0
Jul 8 '11 at 15:20
add a comment |Â
up vote
3
down vote
sed
can be scary, but simple character to string substitution with sed
isn't really too bad.
To substitute x
with the string bar
, do:
something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'
if it's a file you're wanting to process, you can use:
sed -e 's/x/bar/g' filename
(The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)
1
I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. notsed
ding when you cancut
.
â user4518
Jul 8 '11 at 14:44
2
how about notcat
ing when you can just usesed
â xenoterracide
Jul 8 '11 at 15:44
1
@xenoterracide: Good point, edited answer.
â LawrenceC
Jul 8 '11 at 16:02
The only thing that I don't like here is that becomes quite difficult to replace a newlinen
with a string like ` ; ` usingsed
: sed: How can I replace a newline (n)? - Stack Overflow notes that plains///
doesn't work, and you have to go into a "pattern space" syntax...
â sdaau
Jan 24 '15 at 3:12
add a comment |Â
up vote
3
down vote
sed
can be scary, but simple character to string substitution with sed
isn't really too bad.
To substitute x
with the string bar
, do:
something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'
if it's a file you're wanting to process, you can use:
sed -e 's/x/bar/g' filename
(The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)
1
I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. notsed
ding when you cancut
.
â user4518
Jul 8 '11 at 14:44
2
how about notcat
ing when you can just usesed
â xenoterracide
Jul 8 '11 at 15:44
1
@xenoterracide: Good point, edited answer.
â LawrenceC
Jul 8 '11 at 16:02
The only thing that I don't like here is that becomes quite difficult to replace a newlinen
with a string like ` ; ` usingsed
: sed: How can I replace a newline (n)? - Stack Overflow notes that plains///
doesn't work, and you have to go into a "pattern space" syntax...
â sdaau
Jan 24 '15 at 3:12
add a comment |Â
up vote
3
down vote
up vote
3
down vote
sed
can be scary, but simple character to string substitution with sed
isn't really too bad.
To substitute x
with the string bar
, do:
something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'
if it's a file you're wanting to process, you can use:
sed -e 's/x/bar/g' filename
(The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)
sed
can be scary, but simple character to string substitution with sed
isn't really too bad.
To substitute x
with the string bar
, do:
something-that-outputs-something-on-stdout | sed -e 's/x/bar/g'
if it's a file you're wanting to process, you can use:
sed -e 's/x/bar/g' filename
(The "g" at the end means "global" which means substitute all occurrences in the line and not just the first one)
edited Sep 3 at 13:53
Communityâ¦
1
1
answered Jul 8 '11 at 14:41
LawrenceC
8,31722240
8,31722240
1
I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. notsed
ding when you cancut
.
â user4518
Jul 8 '11 at 14:44
2
how about notcat
ing when you can just usesed
â xenoterracide
Jul 8 '11 at 15:44
1
@xenoterracide: Good point, edited answer.
â LawrenceC
Jul 8 '11 at 16:02
The only thing that I don't like here is that becomes quite difficult to replace a newlinen
with a string like ` ; ` usingsed
: sed: How can I replace a newline (n)? - Stack Overflow notes that plains///
doesn't work, and you have to go into a "pattern space" syntax...
â sdaau
Jan 24 '15 at 3:12
add a comment |Â
1
I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. notsed
ding when you cancut
.
â user4518
Jul 8 '11 at 14:44
2
how about notcat
ing when you can just usesed
â xenoterracide
Jul 8 '11 at 15:44
1
@xenoterracide: Good point, edited answer.
â LawrenceC
Jul 8 '11 at 16:02
The only thing that I don't like here is that becomes quite difficult to replace a newlinen
with a string like ` ; ` usingsed
: sed: How can I replace a newline (n)? - Stack Overflow notes that plains///
doesn't work, and you have to go into a "pattern space" syntax...
â sdaau
Jan 24 '15 at 3:12
1
1
I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not
sed
ding when you can cut
.â user4518
Jul 8 '11 at 14:44
I'm not scared of sed; I embrace it. The reason I'm asking is that using simpler tools seems to be generally encouraged, e.g. not
sed
ding when you can cut
.â user4518
Jul 8 '11 at 14:44
2
2
how about not
cat
ing when you can just use sed
â xenoterracide
Jul 8 '11 at 15:44
how about not
cat
ing when you can just use sed
â xenoterracide
Jul 8 '11 at 15:44
1
1
@xenoterracide: Good point, edited answer.
â LawrenceC
Jul 8 '11 at 16:02
@xenoterracide: Good point, edited answer.
â LawrenceC
Jul 8 '11 at 16:02
The only thing that I don't like here is that becomes quite difficult to replace a newline
n
with a string like ` ; ` using sed
: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s///
doesn't work, and you have to go into a "pattern space" syntax...â sdaau
Jan 24 '15 at 3:12
The only thing that I don't like here is that becomes quite difficult to replace a newline
n
with a string like ` ; ` using sed
: sed: How can I replace a newline (n)? - Stack Overflow notes that plain s///
doesn't work, and you have to go into a "pattern space" syntax...â sdaau
Jan 24 '15 at 3:12
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f16281%2fis-the-command-tr-capable-of-translating-characters-into-longer-strings%23new-answer', 'question_page');
);
Post as a guest
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
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
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