Simple sed replacement of tabs mysteriously failing
Clash Royale CLAN TAG#URR8PPP
up vote
30
down vote
favorite
This ought to be really simple, but for some reason it is not working:
sed -i.bak -E 's/t/ /' file.txt
Instead of replacing tab characters, it's replacing t
characters. I've tried every variation on this I could think of, playing with quoting, etc. I've Googled and found everyone else using pretty similar expressions and they seem to work for them.
The -E
is an OS X thing. I thought the failure might be a result of some weird quirk of OS X's sed
, so I tried it with Ruby as well (without the -i
), and got the same result:
ruby -pe '$_.gsub!(/t/," ")' < file.txt > file.new
I'm using Bash 3.2.51 on OS X, and iTerm, although I can't see how any of those could be terribly relevant. I haven't set any weird environment variables, though I can post any that you think might be relevant.
What could be wrong?
UPDATE: I must have made some other mistake or typo when I tried the Ruby version, since Gilles points out that it does work (and I've never had him steer me wrong!). I'm not sure what happened, but I'm pretty sure it must have been my mistake.
sed osx regular-expression
add a comment |Â
up vote
30
down vote
favorite
This ought to be really simple, but for some reason it is not working:
sed -i.bak -E 's/t/ /' file.txt
Instead of replacing tab characters, it's replacing t
characters. I've tried every variation on this I could think of, playing with quoting, etc. I've Googled and found everyone else using pretty similar expressions and they seem to work for them.
The -E
is an OS X thing. I thought the failure might be a result of some weird quirk of OS X's sed
, so I tried it with Ruby as well (without the -i
), and got the same result:
ruby -pe '$_.gsub!(/t/," ")' < file.txt > file.new
I'm using Bash 3.2.51 on OS X, and iTerm, although I can't see how any of those could be terribly relevant. I haven't set any weird environment variables, though I can post any that you think might be relevant.
What could be wrong?
UPDATE: I must have made some other mistake or typo when I tried the Ruby version, since Gilles points out that it does work (and I've never had him steer me wrong!). I'm not sure what happened, but I'm pretty sure it must have been my mistake.
sed osx regular-expression
5
May be you should try to replace thet
in thesed
statement withCTRL-V<TAB>
where<TAB>
is the tab key andCTRL-V
is control key andv
pressed together.
â unxnut
Jul 18 '14 at 14:25
if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
â ctrl-alt-delor
Jul 18 '14 at 16:44
add a comment |Â
up vote
30
down vote
favorite
up vote
30
down vote
favorite
This ought to be really simple, but for some reason it is not working:
sed -i.bak -E 's/t/ /' file.txt
Instead of replacing tab characters, it's replacing t
characters. I've tried every variation on this I could think of, playing with quoting, etc. I've Googled and found everyone else using pretty similar expressions and they seem to work for them.
The -E
is an OS X thing. I thought the failure might be a result of some weird quirk of OS X's sed
, so I tried it with Ruby as well (without the -i
), and got the same result:
ruby -pe '$_.gsub!(/t/," ")' < file.txt > file.new
I'm using Bash 3.2.51 on OS X, and iTerm, although I can't see how any of those could be terribly relevant. I haven't set any weird environment variables, though I can post any that you think might be relevant.
What could be wrong?
UPDATE: I must have made some other mistake or typo when I tried the Ruby version, since Gilles points out that it does work (and I've never had him steer me wrong!). I'm not sure what happened, but I'm pretty sure it must have been my mistake.
sed osx regular-expression
This ought to be really simple, but for some reason it is not working:
sed -i.bak -E 's/t/ /' file.txt
Instead of replacing tab characters, it's replacing t
characters. I've tried every variation on this I could think of, playing with quoting, etc. I've Googled and found everyone else using pretty similar expressions and they seem to work for them.
The -E
is an OS X thing. I thought the failure might be a result of some weird quirk of OS X's sed
, so I tried it with Ruby as well (without the -i
), and got the same result:
ruby -pe '$_.gsub!(/t/," ")' < file.txt > file.new
I'm using Bash 3.2.51 on OS X, and iTerm, although I can't see how any of those could be terribly relevant. I haven't set any weird environment variables, though I can post any that you think might be relevant.
What could be wrong?
UPDATE: I must have made some other mistake or typo when I tried the Ruby version, since Gilles points out that it does work (and I've never had him steer me wrong!). I'm not sure what happened, but I'm pretty sure it must have been my mistake.
sed osx regular-expression
sed osx regular-expression
edited Jan 13 '16 at 23:35
don_crissti
48.2k15127157
48.2k15127157
asked Jul 18 '14 at 14:15
iconoclast
3,70463668
3,70463668
5
May be you should try to replace thet
in thesed
statement withCTRL-V<TAB>
where<TAB>
is the tab key andCTRL-V
is control key andv
pressed together.
â unxnut
Jul 18 '14 at 14:25
if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
â ctrl-alt-delor
Jul 18 '14 at 16:44
add a comment |Â
5
May be you should try to replace thet
in thesed
statement withCTRL-V<TAB>
where<TAB>
is the tab key andCTRL-V
is control key andv
pressed together.
â unxnut
Jul 18 '14 at 14:25
if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
â ctrl-alt-delor
Jul 18 '14 at 16:44
5
5
May be you should try to replace the
t
in the sed
statement with CTRL-V<TAB>
where <TAB>
is the tab key and CTRL-V
is control key and v
pressed together.â unxnut
Jul 18 '14 at 14:25
May be you should try to replace the
t
in the sed
statement with CTRL-V<TAB>
where <TAB>
is the tab key and CTRL-V
is control key and v
pressed together.â unxnut
Jul 18 '14 at 14:25
if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
â ctrl-alt-delor
Jul 18 '14 at 16:44
if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
â ctrl-alt-delor
Jul 18 '14 at 16:44
add a comment |Â
8 Answers
8
active
oldest
votes
up vote
49
down vote
accepted
The syntax t
for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t
for tab and instead treats t
as meaning backslash followed by t
.
There are many solutions, such as:
Use a literal tab character.
sed -i.bak 's/ / /' file.txt
Use
tr
orprintf
to produce a tab character.sed -i.bak "s/$(printf 't')"/ /' file.txt
sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txtUse bash's string syntax allowing backslash escapes.
sed -i.bak $'s/t/ /' file.txt
Use Perl, Python or Ruby. The Ruby snippet that you posted does work.
For sed scripts which are contained in a...sed
script (used via-f
option), the literal tab characters seem the only possibility to me. When editing this with vim,set noexpandtab
is important.
â Tobias
Jan 11 at 11:46
Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use thattr
technique if you want your coworker to stab you in the face when they read your script.
â Bruno Bronosky
Sep 6 at 18:50
add a comment |Â
up vote
11
down vote
Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:
sed -i.bak -E $'s/t/ /' file.txt
1
Also called "ANSI-C" quoting if others want to look up more info about it.
â wisbucky
Jun 24 '16 at 23:28
2
Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
â jornane
Nov 15 '16 at 14:43
add a comment |Â
up vote
1
down vote
sed -i $'s/t/ /g' file.txt
works for me on OS X and is the same command i use on linux all the time.
Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
â Kusalananda
Sep 26 '17 at 19:33
add a comment |Â
up vote
0
down vote
If you want a more powerful sed
(supporting t
and more) than the one on OS X, install GNU sed.
Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X'ssed
is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
â iconoclast
Jul 18 '14 at 15:36
With Ruby, you'll have to use only one backslash:ruby -pe '$_.gsub!(/t/," ")' < file.txt
â vinc17
Jul 18 '14 at 15:45
add a comment |Â
up vote
0
down vote
As noted, not all sed
implementations support the notation of t
as a horizontal tab.
You can easily achieve your substitution with:
perl -pi.old -e 'st+ g' file.txt
This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic /
making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).
The +
says one or more repetitions of a tab character are to be replaced. The g
modifier enables global replacements throughout the end of each line.
add a comment |Â
up vote
0
down vote
sed -i.bak -E 's/\t/ /' file.txt
add a comment |Â
up vote
-1
down vote
I'm surprised no one suggested the very simple solution of:
sed -i.bak -E 's/\t/ /' file.txt
That should do the trick.
You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...
Why three backslashes specifically?
â Michael Homer
Jan 14 '16 at 0:19
3
If I use GNUsed
, onesed
does not support this syntax for tabs.
â iconoclast
Jan 16 '16 at 16:43
Does not work on my El Capitan.
â Franklin Yu
Jun 19 '16 at 6:46
add a comment |Â
up vote
-4
down vote
This worked for me.
sed -e 's/[t]/ /g'
3
This is because you use GNUsed
. This is not what the OP uses.
â Kusalananda
Sep 26 '17 at 19:34
add a comment |Â
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
49
down vote
accepted
The syntax t
for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t
for tab and instead treats t
as meaning backslash followed by t
.
There are many solutions, such as:
Use a literal tab character.
sed -i.bak 's/ / /' file.txt
Use
tr
orprintf
to produce a tab character.sed -i.bak "s/$(printf 't')"/ /' file.txt
sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txtUse bash's string syntax allowing backslash escapes.
sed -i.bak $'s/t/ /' file.txt
Use Perl, Python or Ruby. The Ruby snippet that you posted does work.
For sed scripts which are contained in a...sed
script (used via-f
option), the literal tab characters seem the only possibility to me. When editing this with vim,set noexpandtab
is important.
â Tobias
Jan 11 at 11:46
Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use thattr
technique if you want your coworker to stab you in the face when they read your script.
â Bruno Bronosky
Sep 6 at 18:50
add a comment |Â
up vote
49
down vote
accepted
The syntax t
for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t
for tab and instead treats t
as meaning backslash followed by t
.
There are many solutions, such as:
Use a literal tab character.
sed -i.bak 's/ / /' file.txt
Use
tr
orprintf
to produce a tab character.sed -i.bak "s/$(printf 't')"/ /' file.txt
sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txtUse bash's string syntax allowing backslash escapes.
sed -i.bak $'s/t/ /' file.txt
Use Perl, Python or Ruby. The Ruby snippet that you posted does work.
For sed scripts which are contained in a...sed
script (used via-f
option), the literal tab characters seem the only possibility to me. When editing this with vim,set noexpandtab
is important.
â Tobias
Jan 11 at 11:46
Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use thattr
technique if you want your coworker to stab you in the face when they read your script.
â Bruno Bronosky
Sep 6 at 18:50
add a comment |Â
up vote
49
down vote
accepted
up vote
49
down vote
accepted
The syntax t
for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t
for tab and instead treats t
as meaning backslash followed by t
.
There are many solutions, such as:
Use a literal tab character.
sed -i.bak 's/ / /' file.txt
Use
tr
orprintf
to produce a tab character.sed -i.bak "s/$(printf 't')"/ /' file.txt
sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txtUse bash's string syntax allowing backslash escapes.
sed -i.bak $'s/t/ /' file.txt
Use Perl, Python or Ruby. The Ruby snippet that you posted does work.
The syntax t
for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t
for tab and instead treats t
as meaning backslash followed by t
.
There are many solutions, such as:
Use a literal tab character.
sed -i.bak 's/ / /' file.txt
Use
tr
orprintf
to produce a tab character.sed -i.bak "s/$(printf 't')"/ /' file.txt
sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txtUse bash's string syntax allowing backslash escapes.
sed -i.bak $'s/t/ /' file.txt
Use Perl, Python or Ruby. The Ruby snippet that you posted does work.
edited Jul 19 '14 at 5:01
Cristian Ciupitu
2,01111520
2,01111520
answered Jul 18 '14 at 23:02
Gilles
516k12210271556
516k12210271556
For sed scripts which are contained in a...sed
script (used via-f
option), the literal tab characters seem the only possibility to me. When editing this with vim,set noexpandtab
is important.
â Tobias
Jan 11 at 11:46
Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use thattr
technique if you want your coworker to stab you in the face when they read your script.
â Bruno Bronosky
Sep 6 at 18:50
add a comment |Â
For sed scripts which are contained in a...sed
script (used via-f
option), the literal tab characters seem the only possibility to me. When editing this with vim,set noexpandtab
is important.
â Tobias
Jan 11 at 11:46
Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use thattr
technique if you want your coworker to stab you in the face when they read your script.
â Bruno Bronosky
Sep 6 at 18:50
For sed scripts which are contained in a
...sed
script (used via -f
option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab
is important.â Tobias
Jan 11 at 11:46
For sed scripts which are contained in a
...sed
script (used via -f
option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab
is important.â Tobias
Jan 11 at 11:46
Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that
tr
technique if you want your coworker to stab you in the face when they read your script.â Bruno Bronosky
Sep 6 at 18:50
Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that
tr
technique if you want your coworker to stab you in the face when they read your script.â Bruno Bronosky
Sep 6 at 18:50
add a comment |Â
up vote
11
down vote
Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:
sed -i.bak -E $'s/t/ /' file.txt
1
Also called "ANSI-C" quoting if others want to look up more info about it.
â wisbucky
Jun 24 '16 at 23:28
2
Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
â jornane
Nov 15 '16 at 14:43
add a comment |Â
up vote
11
down vote
Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:
sed -i.bak -E $'s/t/ /' file.txt
1
Also called "ANSI-C" quoting if others want to look up more info about it.
â wisbucky
Jun 24 '16 at 23:28
2
Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
â jornane
Nov 15 '16 at 14:43
add a comment |Â
up vote
11
down vote
up vote
11
down vote
Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:
sed -i.bak -E $'s/t/ /' file.txt
Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:
sed -i.bak -E $'s/t/ /' file.txt
answered Jul 18 '14 at 16:31
Cristian Ciupitu
2,01111520
2,01111520
1
Also called "ANSI-C" quoting if others want to look up more info about it.
â wisbucky
Jun 24 '16 at 23:28
2
Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
â jornane
Nov 15 '16 at 14:43
add a comment |Â
1
Also called "ANSI-C" quoting if others want to look up more info about it.
â wisbucky
Jun 24 '16 at 23:28
2
Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
â jornane
Nov 15 '16 at 14:43
1
1
Also called "ANSI-C" quoting if others want to look up more info about it.
â wisbucky
Jun 24 '16 at 23:28
Also called "ANSI-C" quoting if others want to look up more info about it.
â wisbucky
Jun 24 '16 at 23:28
2
2
Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
â jornane
Nov 15 '16 at 14:43
Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
â jornane
Nov 15 '16 at 14:43
add a comment |Â
up vote
1
down vote
sed -i $'s/t/ /g' file.txt
works for me on OS X and is the same command i use on linux all the time.
Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
â Kusalananda
Sep 26 '17 at 19:33
add a comment |Â
up vote
1
down vote
sed -i $'s/t/ /g' file.txt
works for me on OS X and is the same command i use on linux all the time.
Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
â Kusalananda
Sep 26 '17 at 19:33
add a comment |Â
up vote
1
down vote
up vote
1
down vote
sed -i $'s/t/ /g' file.txt
works for me on OS X and is the same command i use on linux all the time.
sed -i $'s/t/ /g' file.txt
works for me on OS X and is the same command i use on linux all the time.
edited Oct 4 '16 at 18:31
Tomasz
8,57052761
8,57052761
answered Oct 4 '16 at 18:12
user193377
212
212
Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
â Kusalananda
Sep 26 '17 at 19:33
add a comment |Â
Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
â Kusalananda
Sep 26 '17 at 19:33
Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
â Kusalananda
Sep 26 '17 at 19:33
Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
â Kusalananda
Sep 26 '17 at 19:33
add a comment |Â
up vote
0
down vote
If you want a more powerful sed
(supporting t
and more) than the one on OS X, install GNU sed.
Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X'ssed
is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
â iconoclast
Jul 18 '14 at 15:36
With Ruby, you'll have to use only one backslash:ruby -pe '$_.gsub!(/t/," ")' < file.txt
â vinc17
Jul 18 '14 at 15:45
add a comment |Â
up vote
0
down vote
If you want a more powerful sed
(supporting t
and more) than the one on OS X, install GNU sed.
Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X'ssed
is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
â iconoclast
Jul 18 '14 at 15:36
With Ruby, you'll have to use only one backslash:ruby -pe '$_.gsub!(/t/," ")' < file.txt
â vinc17
Jul 18 '14 at 15:45
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If you want a more powerful sed
(supporting t
and more) than the one on OS X, install GNU sed.
If you want a more powerful sed
(supporting t
and more) than the one on OS X, install GNU sed.
answered Jul 18 '14 at 15:31
vinc17
8,6791636
8,6791636
Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X'ssed
is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
â iconoclast
Jul 18 '14 at 15:36
With Ruby, you'll have to use only one backslash:ruby -pe '$_.gsub!(/t/," ")' < file.txt
â vinc17
Jul 18 '14 at 15:45
add a comment |Â
Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X'ssed
is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
â iconoclast
Jul 18 '14 at 15:36
With Ruby, you'll have to use only one backslash:ruby -pe '$_.gsub!(/t/," ")' < file.txt
â vinc17
Jul 18 '14 at 15:45
Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's
sed
is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.â iconoclast
Jul 18 '14 at 15:36
Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's
sed
is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.â iconoclast
Jul 18 '14 at 15:36
With Ruby, you'll have to use only one backslash:
ruby -pe '$_.gsub!(/t/," ")' < file.txt
â vinc17
Jul 18 '14 at 15:45
With Ruby, you'll have to use only one backslash:
ruby -pe '$_.gsub!(/t/," ")' < file.txt
â vinc17
Jul 18 '14 at 15:45
add a comment |Â
up vote
0
down vote
As noted, not all sed
implementations support the notation of t
as a horizontal tab.
You can easily achieve your substitution with:
perl -pi.old -e 'st+ g' file.txt
This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic /
making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).
The +
says one or more repetitions of a tab character are to be replaced. The g
modifier enables global replacements throughout the end of each line.
add a comment |Â
up vote
0
down vote
As noted, not all sed
implementations support the notation of t
as a horizontal tab.
You can easily achieve your substitution with:
perl -pi.old -e 'st+ g' file.txt
This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic /
making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).
The +
says one or more repetitions of a tab character are to be replaced. The g
modifier enables global replacements throughout the end of each line.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
As noted, not all sed
implementations support the notation of t
as a horizontal tab.
You can easily achieve your substitution with:
perl -pi.old -e 'st+ g' file.txt
This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic /
making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).
The +
says one or more repetitions of a tab character are to be replaced. The g
modifier enables global replacements throughout the end of each line.
As noted, not all sed
implementations support the notation of t
as a horizontal tab.
You can easily achieve your substitution with:
perl -pi.old -e 'st+ g' file.txt
This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic /
making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).
The +
says one or more repetitions of a tab character are to be replaced. The g
modifier enables global replacements throughout the end of each line.
edited Jul 18 '14 at 15:48
answered Jul 18 '14 at 15:42
JRFerguson
9,34032329
9,34032329
add a comment |Â
add a comment |Â
up vote
0
down vote
sed -i.bak -E 's/\t/ /' file.txt
add a comment |Â
up vote
0
down vote
sed -i.bak -E 's/\t/ /' file.txt
add a comment |Â
up vote
0
down vote
up vote
0
down vote
sed -i.bak -E 's/\t/ /' file.txt
sed -i.bak -E 's/\t/ /' file.txt
answered 3 mins ago
sjas
24837
24837
add a comment |Â
add a comment |Â
up vote
-1
down vote
I'm surprised no one suggested the very simple solution of:
sed -i.bak -E 's/\t/ /' file.txt
That should do the trick.
You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...
Why three backslashes specifically?
â Michael Homer
Jan 14 '16 at 0:19
3
If I use GNUsed
, onesed
does not support this syntax for tabs.
â iconoclast
Jan 16 '16 at 16:43
Does not work on my El Capitan.
â Franklin Yu
Jun 19 '16 at 6:46
add a comment |Â
up vote
-1
down vote
I'm surprised no one suggested the very simple solution of:
sed -i.bak -E 's/\t/ /' file.txt
That should do the trick.
You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...
Why three backslashes specifically?
â Michael Homer
Jan 14 '16 at 0:19
3
If I use GNUsed
, onesed
does not support this syntax for tabs.
â iconoclast
Jan 16 '16 at 16:43
Does not work on my El Capitan.
â Franklin Yu
Jun 19 '16 at 6:46
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
I'm surprised no one suggested the very simple solution of:
sed -i.bak -E 's/\t/ /' file.txt
That should do the trick.
You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...
I'm surprised no one suggested the very simple solution of:
sed -i.bak -E 's/\t/ /' file.txt
That should do the trick.
You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...
answered Jan 13 '16 at 23:31
Vas
1
1
Why three backslashes specifically?
â Michael Homer
Jan 14 '16 at 0:19
3
If I use GNUsed
, onesed
does not support this syntax for tabs.
â iconoclast
Jan 16 '16 at 16:43
Does not work on my El Capitan.
â Franklin Yu
Jun 19 '16 at 6:46
add a comment |Â
Why three backslashes specifically?
â Michael Homer
Jan 14 '16 at 0:19
3
If I use GNUsed
, onesed
does not support this syntax for tabs.
â iconoclast
Jan 16 '16 at 16:43
Does not work on my El Capitan.
â Franklin Yu
Jun 19 '16 at 6:46
Why three backslashes specifically?
â Michael Homer
Jan 14 '16 at 0:19
Why three backslashes specifically?
â Michael Homer
Jan 14 '16 at 0:19
3
3
If I use GNU
sed
, one
is enough, as no escaping is necessary. The problem is that BSD sed
does not support this syntax for tabs.â iconoclast
Jan 16 '16 at 16:43
If I use GNU
sed
, one
is enough, as no escaping is necessary. The problem is that BSD sed
does not support this syntax for tabs.â iconoclast
Jan 16 '16 at 16:43
Does not work on my El Capitan.
â Franklin Yu
Jun 19 '16 at 6:46
Does not work on my El Capitan.
â Franklin Yu
Jun 19 '16 at 6:46
add a comment |Â
up vote
-4
down vote
This worked for me.
sed -e 's/[t]/ /g'
3
This is because you use GNUsed
. This is not what the OP uses.
â Kusalananda
Sep 26 '17 at 19:34
add a comment |Â
up vote
-4
down vote
This worked for me.
sed -e 's/[t]/ /g'
3
This is because you use GNUsed
. This is not what the OP uses.
â Kusalananda
Sep 26 '17 at 19:34
add a comment |Â
up vote
-4
down vote
up vote
-4
down vote
This worked for me.
sed -e 's/[t]/ /g'
This worked for me.
sed -e 's/[t]/ /g'
answered Sep 26 '17 at 19:27
RChristensen
1
1
3
This is because you use GNUsed
. This is not what the OP uses.
â Kusalananda
Sep 26 '17 at 19:34
add a comment |Â
3
This is because you use GNUsed
. This is not what the OP uses.
â Kusalananda
Sep 26 '17 at 19:34
3
3
This is because you use GNU
sed
. This is not what the OP uses.â Kusalananda
Sep 26 '17 at 19:34
This is because you use GNU
sed
. This is not what the OP uses.â Kusalananda
Sep 26 '17 at 19:34
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%2f145299%2fsimple-sed-replacement-of-tabs-mysteriously-failing%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
5
May be you should try to replace the
t
in thesed
statement withCTRL-V<TAB>
where<TAB>
is the tab key andCTRL-V
is control key andv
pressed together.â unxnut
Jul 18 '14 at 14:25
if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
â ctrl-alt-delor
Jul 18 '14 at 16:44