replace indentation tabs with fixed number of spaces per tab The Next CEO of Stack OverflowThere must be a better way to replace single newlines only?Count tabs per line in text file with utilsWhat command(s) will feed a tab-delimited text file and cut each line to 80 characters?Selectively correcting LFs to CRLFsRemove fields containing specific stringReplace any number of tabs and spaces with single new line in Linux?Given a starting key token followed by n tokens, interleave the n tokens with the keyWhat does `pr -e` mean?Layout tab/spacesautomatically repair rsnapshot config: replace spaces with tabs
Running a General Election and the European Elections together
Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis
Tactics for judging if a printed image will be bright enough?
Why, when going from special to general relativity, do we just replace partial derivatives with covariant derivatives?
Prepend last line of stdin to entire stdin
Why didn't Khan get resurrected in the Genesis Explosion?
Would a completely good Muggle be able to use a wand?
How a 64-bit process virtual address space is divided in Linux?
What flight has the highest ratio of time difference to flight time?
How to count occurrences of text in a file?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
Is a distribution that is normal, but highly skewed considered Gaussian?
Why did CATV standarize in 75 ohms and everyone else in 50?
Domestic-to-international connection at Orlando (MCO)
Why don't programming languages automatically manage the synchronous/asynchronous problem?
How to place nodes around a circle from some initial angle?
Do I need to write [sic] when a number is less than 10 but isn't written out?
Reference request: Grassmannian and Plucker coordinates in type B, C, D
Why does standard notation not preserve intervals (visually)
Does soap repel water?
What was the first Unix version to run on a microcomputer?
RigExpert AA-35 - Interpreting The Information
Is it professional to write unrelated content in an almost-empty email?
Solving system of ODEs with extra parameter
replace indentation tabs with fixed number of spaces per tab
The Next CEO of Stack OverflowThere must be a better way to replace single newlines only?Count tabs per line in text file with utilsWhat command(s) will feed a tab-delimited text file and cut each line to 80 characters?Selectively correcting LFs to CRLFsRemove fields containing specific stringReplace any number of tabs and spaces with single new line in Linux?Given a starting key token followed by n tokens, interleave the n tokens with the keyWhat does `pr -e` mean?Layout tab/spacesautomatically repair rsnapshot config: replace spaces with tabs
Is it possible to replace only "leading tabs" with a set number of spaces per tab? I'm defining a leading tab as one that is only preceded by tabs or spaces. I'm defining a leading or an indentation tab as one that is only preceded by whitespace.
I have the following line in my .nexrc, cribbed from this one. It runs the lines between the lines containing the mark a and the mark b through a filter. that replaces tabs.
map 2 'a!'b pr -te2
However, it replaces non-leading tabs with two spaces well, which is not ideal since a literal tab is sometimes needed.
Is there an easy and portable way to replace leading tabs only with a set number of spaces per tab?
The following perl script (which could be collapsed to a one-liner) does the job of replacing leading tabs with a width supplied on the command line, but it relies on the availability of Perl and takes quadratic time (if the file consists of nothing but tabs).
#!/usr/bin/env perl
use strict;
use warnings;
my ($width) = @ARGV;
$width //= 2;
my $replacement = ' ' x $width;
while (<>)
while (s/^(s*)t/$1$replacement/g)
print;
Here's an example of what I want to happen. ^I refers to a tab byte and # is a zero-width marker for the start of a line. The line below is a byte-ruler.
BEFORE:
#^I^I ^Ia^Ib
# 1 23 45 67
AFTER (tab width of 2)
# a^Ib
#12345678 90
AFTER (tab width of 4)
# a^Ib
#12345678901234 56
text-processing text-formatting
|
show 7 more comments
Is it possible to replace only "leading tabs" with a set number of spaces per tab? I'm defining a leading tab as one that is only preceded by tabs or spaces. I'm defining a leading or an indentation tab as one that is only preceded by whitespace.
I have the following line in my .nexrc, cribbed from this one. It runs the lines between the lines containing the mark a and the mark b through a filter. that replaces tabs.
map 2 'a!'b pr -te2
However, it replaces non-leading tabs with two spaces well, which is not ideal since a literal tab is sometimes needed.
Is there an easy and portable way to replace leading tabs only with a set number of spaces per tab?
The following perl script (which could be collapsed to a one-liner) does the job of replacing leading tabs with a width supplied on the command line, but it relies on the availability of Perl and takes quadratic time (if the file consists of nothing but tabs).
#!/usr/bin/env perl
use strict;
use warnings;
my ($width) = @ARGV;
$width //= 2;
my $replacement = ' ' x $width;
while (<>)
while (s/^(s*)t/$1$replacement/g)
print;
Here's an example of what I want to happen. ^I refers to a tab byte and # is a zero-width marker for the start of a line. The line below is a byte-ruler.
BEFORE:
#^I^I ^Ia^Ib
# 1 23 45 67
AFTER (tab width of 2)
# a^Ib
#12345678 90
AFTER (tab width of 4)
# a^Ib
#12345678901234 56
text-processing text-formatting
2
Are you looking for an nvi solution, or a general command-line solution?
– Sparhawk
2 days ago
1
expand(from GNU coreutils) would seem to be the obvious generic solution e.g.expand -it3to replace each initial tab with 3 spaces
– steeldriver
2 days ago
sed 's/^tt*/ /' replace leading tabs with 2 spaces
– ctac_
2 days ago
sorry, I meant replacing leading tabs with two spaces per tab.
– Gregory Nisbet
2 days ago
@GregoryNisbet If you want i read it add @ before my name. You can try sed ':A;s/(^t)(t*)/2 /;tA'
– ctac_
2 days ago
|
show 7 more comments
Is it possible to replace only "leading tabs" with a set number of spaces per tab? I'm defining a leading tab as one that is only preceded by tabs or spaces. I'm defining a leading or an indentation tab as one that is only preceded by whitespace.
I have the following line in my .nexrc, cribbed from this one. It runs the lines between the lines containing the mark a and the mark b through a filter. that replaces tabs.
map 2 'a!'b pr -te2
However, it replaces non-leading tabs with two spaces well, which is not ideal since a literal tab is sometimes needed.
Is there an easy and portable way to replace leading tabs only with a set number of spaces per tab?
The following perl script (which could be collapsed to a one-liner) does the job of replacing leading tabs with a width supplied on the command line, but it relies on the availability of Perl and takes quadratic time (if the file consists of nothing but tabs).
#!/usr/bin/env perl
use strict;
use warnings;
my ($width) = @ARGV;
$width //= 2;
my $replacement = ' ' x $width;
while (<>)
while (s/^(s*)t/$1$replacement/g)
print;
Here's an example of what I want to happen. ^I refers to a tab byte and # is a zero-width marker for the start of a line. The line below is a byte-ruler.
BEFORE:
#^I^I ^Ia^Ib
# 1 23 45 67
AFTER (tab width of 2)
# a^Ib
#12345678 90
AFTER (tab width of 4)
# a^Ib
#12345678901234 56
text-processing text-formatting
Is it possible to replace only "leading tabs" with a set number of spaces per tab? I'm defining a leading tab as one that is only preceded by tabs or spaces. I'm defining a leading or an indentation tab as one that is only preceded by whitespace.
I have the following line in my .nexrc, cribbed from this one. It runs the lines between the lines containing the mark a and the mark b through a filter. that replaces tabs.
map 2 'a!'b pr -te2
However, it replaces non-leading tabs with two spaces well, which is not ideal since a literal tab is sometimes needed.
Is there an easy and portable way to replace leading tabs only with a set number of spaces per tab?
The following perl script (which could be collapsed to a one-liner) does the job of replacing leading tabs with a width supplied on the command line, but it relies on the availability of Perl and takes quadratic time (if the file consists of nothing but tabs).
#!/usr/bin/env perl
use strict;
use warnings;
my ($width) = @ARGV;
$width //= 2;
my $replacement = ' ' x $width;
while (<>)
while (s/^(s*)t/$1$replacement/g)
print;
Here's an example of what I want to happen. ^I refers to a tab byte and # is a zero-width marker for the start of a line. The line below is a byte-ruler.
BEFORE:
#^I^I ^Ia^Ib
# 1 23 45 67
AFTER (tab width of 2)
# a^Ib
#12345678 90
AFTER (tab width of 4)
# a^Ib
#12345678901234 56
text-processing text-formatting
text-processing text-formatting
edited 2 days ago
Gregory Nisbet
asked 2 days ago
Gregory NisbetGregory Nisbet
1,4981020
1,4981020
2
Are you looking for an nvi solution, or a general command-line solution?
– Sparhawk
2 days ago
1
expand(from GNU coreutils) would seem to be the obvious generic solution e.g.expand -it3to replace each initial tab with 3 spaces
– steeldriver
2 days ago
sed 's/^tt*/ /' replace leading tabs with 2 spaces
– ctac_
2 days ago
sorry, I meant replacing leading tabs with two spaces per tab.
– Gregory Nisbet
2 days ago
@GregoryNisbet If you want i read it add @ before my name. You can try sed ':A;s/(^t)(t*)/2 /;tA'
– ctac_
2 days ago
|
show 7 more comments
2
Are you looking for an nvi solution, or a general command-line solution?
– Sparhawk
2 days ago
1
expand(from GNU coreutils) would seem to be the obvious generic solution e.g.expand -it3to replace each initial tab with 3 spaces
– steeldriver
2 days ago
sed 's/^tt*/ /' replace leading tabs with 2 spaces
– ctac_
2 days ago
sorry, I meant replacing leading tabs with two spaces per tab.
– Gregory Nisbet
2 days ago
@GregoryNisbet If you want i read it add @ before my name. You can try sed ':A;s/(^t)(t*)/2 /;tA'
– ctac_
2 days ago
2
2
Are you looking for an nvi solution, or a general command-line solution?
– Sparhawk
2 days ago
Are you looking for an nvi solution, or a general command-line solution?
– Sparhawk
2 days ago
1
1
expand (from GNU coreutils) would seem to be the obvious generic solution e.g. expand -it3 to replace each initial tab with 3 spaces– steeldriver
2 days ago
expand (from GNU coreutils) would seem to be the obvious generic solution e.g. expand -it3 to replace each initial tab with 3 spaces– steeldriver
2 days ago
sed 's/^tt*/ /' replace leading tabs with 2 spaces
– ctac_
2 days ago
sed 's/^tt*/ /' replace leading tabs with 2 spaces
– ctac_
2 days ago
sorry, I meant replacing leading tabs with two spaces per tab.
– Gregory Nisbet
2 days ago
sorry, I meant replacing leading tabs with two spaces per tab.
– Gregory Nisbet
2 days ago
@GregoryNisbet If you want i read it add @ before my name. You can try sed ':A;s/(^t)(t*)/2 /;tA'
– ctac_
2 days ago
@GregoryNisbet If you want i read it add @ before my name. You can try sed ':A;s/(^t)(t*)/2 /;tA'
– ctac_
2 days ago
|
show 7 more comments
1 Answer
1
active
oldest
votes
If you are looking for a generic solution using other command-line tools, one could use Awk for this.
var=$'tttnewcontent'
echo "$var" | awk ' gsub(/^[t]+/," ",$0) 1'
The command above replaces one more more occurrences of Tab with a single space i.e. 2nd argument in gsub. You can make it configurable though by generating how many spaces you want
awk -v n=5 ' BEGIN spaces = sprintf("%"n"s"," ") gsub(/^[t]+/,spaces,$0) 1'
From the last update, it seems OP wants to replace the number of leading tabs with the exact number of spaces found,
awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1'
The logic is with setting field separator to t, the number of leading tabs can be identified by doing NF-1. With the number of leading tabs identified, use it to generate the same of number of spaces from earlier sprintf() method.
You can see it by verifying the hexdump -C before and after the replacement
echo "$var" | hexdump -C
00000000 09 09 09 6e 65 77 63 6f 6e 74 65 6e 74 0a |...newcontent.|
0000000e
now with the replacement
echo "$var" | awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1' | hexdump -C
00000000 20 20 20 6e 65 77 63 6f 6e 74 65 6e 74 0a | newcontent.|
0000000e
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%2f509149%2freplace-indentation-tabs-with-fixed-number-of-spaces-per-tab%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you are looking for a generic solution using other command-line tools, one could use Awk for this.
var=$'tttnewcontent'
echo "$var" | awk ' gsub(/^[t]+/," ",$0) 1'
The command above replaces one more more occurrences of Tab with a single space i.e. 2nd argument in gsub. You can make it configurable though by generating how many spaces you want
awk -v n=5 ' BEGIN spaces = sprintf("%"n"s"," ") gsub(/^[t]+/,spaces,$0) 1'
From the last update, it seems OP wants to replace the number of leading tabs with the exact number of spaces found,
awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1'
The logic is with setting field separator to t, the number of leading tabs can be identified by doing NF-1. With the number of leading tabs identified, use it to generate the same of number of spaces from earlier sprintf() method.
You can see it by verifying the hexdump -C before and after the replacement
echo "$var" | hexdump -C
00000000 09 09 09 6e 65 77 63 6f 6e 74 65 6e 74 0a |...newcontent.|
0000000e
now with the replacement
echo "$var" | awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1' | hexdump -C
00000000 20 20 20 6e 65 77 63 6f 6e 74 65 6e 74 0a | newcontent.|
0000000e
add a comment |
If you are looking for a generic solution using other command-line tools, one could use Awk for this.
var=$'tttnewcontent'
echo "$var" | awk ' gsub(/^[t]+/," ",$0) 1'
The command above replaces one more more occurrences of Tab with a single space i.e. 2nd argument in gsub. You can make it configurable though by generating how many spaces you want
awk -v n=5 ' BEGIN spaces = sprintf("%"n"s"," ") gsub(/^[t]+/,spaces,$0) 1'
From the last update, it seems OP wants to replace the number of leading tabs with the exact number of spaces found,
awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1'
The logic is with setting field separator to t, the number of leading tabs can be identified by doing NF-1. With the number of leading tabs identified, use it to generate the same of number of spaces from earlier sprintf() method.
You can see it by verifying the hexdump -C before and after the replacement
echo "$var" | hexdump -C
00000000 09 09 09 6e 65 77 63 6f 6e 74 65 6e 74 0a |...newcontent.|
0000000e
now with the replacement
echo "$var" | awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1' | hexdump -C
00000000 20 20 20 6e 65 77 63 6f 6e 74 65 6e 74 0a | newcontent.|
0000000e
add a comment |
If you are looking for a generic solution using other command-line tools, one could use Awk for this.
var=$'tttnewcontent'
echo "$var" | awk ' gsub(/^[t]+/," ",$0) 1'
The command above replaces one more more occurrences of Tab with a single space i.e. 2nd argument in gsub. You can make it configurable though by generating how many spaces you want
awk -v n=5 ' BEGIN spaces = sprintf("%"n"s"," ") gsub(/^[t]+/,spaces,$0) 1'
From the last update, it seems OP wants to replace the number of leading tabs with the exact number of spaces found,
awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1'
The logic is with setting field separator to t, the number of leading tabs can be identified by doing NF-1. With the number of leading tabs identified, use it to generate the same of number of spaces from earlier sprintf() method.
You can see it by verifying the hexdump -C before and after the replacement
echo "$var" | hexdump -C
00000000 09 09 09 6e 65 77 63 6f 6e 74 65 6e 74 0a |...newcontent.|
0000000e
now with the replacement
echo "$var" | awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1' | hexdump -C
00000000 20 20 20 6e 65 77 63 6f 6e 74 65 6e 74 0a | newcontent.|
0000000e
If you are looking for a generic solution using other command-line tools, one could use Awk for this.
var=$'tttnewcontent'
echo "$var" | awk ' gsub(/^[t]+/," ",$0) 1'
The command above replaces one more more occurrences of Tab with a single space i.e. 2nd argument in gsub. You can make it configurable though by generating how many spaces you want
awk -v n=5 ' BEGIN spaces = sprintf("%"n"s"," ") gsub(/^[t]+/,spaces,$0) 1'
From the last update, it seems OP wants to replace the number of leading tabs with the exact number of spaces found,
awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1'
The logic is with setting field separator to t, the number of leading tabs can be identified by doing NF-1. With the number of leading tabs identified, use it to generate the same of number of spaces from earlier sprintf() method.
You can see it by verifying the hexdump -C before and after the replacement
echo "$var" | hexdump -C
00000000 09 09 09 6e 65 77 63 6f 6e 74 65 6e 74 0a |...newcontent.|
0000000e
now with the replacement
echo "$var" | awk -F$'t' ' spaces = sprintf("%"(NF-1)"s"," "); gsub(/^[t]+/,spaces,$0) 1' | hexdump -C
00000000 20 20 20 6e 65 77 63 6f 6e 74 65 6e 74 0a | newcontent.|
0000000e
edited yesterday
answered 2 days ago
InianInian
5,2051529
5,2051529
add a comment |
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%2f509149%2freplace-indentation-tabs-with-fixed-number-of-spaces-per-tab%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
2
Are you looking for an nvi solution, or a general command-line solution?
– Sparhawk
2 days ago
1
expand(from GNU coreutils) would seem to be the obvious generic solution e.g.expand -it3to replace each initial tab with 3 spaces– steeldriver
2 days ago
sed 's/^tt*/ /' replace leading tabs with 2 spaces
– ctac_
2 days ago
sorry, I meant replacing leading tabs with two spaces per tab.
– Gregory Nisbet
2 days ago
@GregoryNisbet If you want i read it add @ before my name. You can try sed ':A;s/(^t)(t*)/2 /;tA'
– ctac_
2 days ago