Replace leading tabs and spaces with sed
I want to replace leading tabs and spaces with something like <TAB>
and <SPACE>
respectively.
But I couldn't figure out how to do it in a single pass of sed
because tabs and spaces in the original file can be intermixed, so simply doing one replacement and than another doesn't work.
Input example (tabs shown as ^):
^^line with tabs
line with spaces
^ ^intermixed
Desired output:
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
bash sed tabs whitespace
add a comment |
I want to replace leading tabs and spaces with something like <TAB>
and <SPACE>
respectively.
But I couldn't figure out how to do it in a single pass of sed
because tabs and spaces in the original file can be intermixed, so simply doing one replacement and than another doesn't work.
Input example (tabs shown as ^):
^^line with tabs
line with spaces
^ ^intermixed
Desired output:
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
bash sed tabs whitespace
add a comment |
I want to replace leading tabs and spaces with something like <TAB>
and <SPACE>
respectively.
But I couldn't figure out how to do it in a single pass of sed
because tabs and spaces in the original file can be intermixed, so simply doing one replacement and than another doesn't work.
Input example (tabs shown as ^):
^^line with tabs
line with spaces
^ ^intermixed
Desired output:
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
bash sed tabs whitespace
I want to replace leading tabs and spaces with something like <TAB>
and <SPACE>
respectively.
But I couldn't figure out how to do it in a single pass of sed
because tabs and spaces in the original file can be intermixed, so simply doing one replacement and than another doesn't work.
Input example (tabs shown as ^):
^^line with tabs
line with spaces
^ ^intermixed
Desired output:
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
bash sed tabs whitespace
bash sed tabs whitespace
asked Feb 12 at 21:41
AmomumAmomum
1034
1034
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I know you said you want to use sed
, which is often a wonderful tool. But where there are choices and loops, I find that awk
outshines it.
#!/usr/bin/gawk -f
{ while (/^s/) {
if (sub(/^ /,"")) printf "<space>";
if (sub(/^t/,"")) printf "<tab>";
}
print;
}
If we create a file input.txt
containing the input example, and name the script replace
, it's run as follows, which produces the desired output.
replace input.txt
UPDATE:
Oops. There's an infinite loop in that code. The sequence s
matches [ tnrfv]
, so if there's a stray form feed, it'll spin forever. But [:blank:]
matches just space and tab, so the second line should be this.
{ while (/^[[:blank:]]/) {
This is a really elegant solution.
– zx485
Feb 13 at 1:26
That is very readable solution! I guess there is no point usingsed
whenawk
solution looks simpler. Thank you!
– Amomum
Feb 13 at 20:21
add a comment |
One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.
echo -e 'ttline withttabs
line with spaces
t tintermixed' | sed -r '
# On the lines that start with tab or space.
/^[t ]/ {
# Put the whole line in the hold space.
h
# Delete all tabs and spaces at the start of line.
s/^[t ]+//
# Exchange pattern and hold spaces.
# This saves the text part to the hold space and
# bring back the original line to the pattern space.
x
# Now let in pattern space only tabs and spaces
# at the start of line (the rest is on hold space).
s/^([t ]+).*/1/
# At least make the substitutions.
s/t/<TAB>/g
s/ /<SPACE>/g
# Add a n (new line) at the end of pattern space,
# then get the content of hold space and append it
# to pattern space.
G
# Delete the extra n added above.
s/n//
}'
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f1405022%2freplace-leading-tabs-and-spaces-with-sed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I know you said you want to use sed
, which is often a wonderful tool. But where there are choices and loops, I find that awk
outshines it.
#!/usr/bin/gawk -f
{ while (/^s/) {
if (sub(/^ /,"")) printf "<space>";
if (sub(/^t/,"")) printf "<tab>";
}
print;
}
If we create a file input.txt
containing the input example, and name the script replace
, it's run as follows, which produces the desired output.
replace input.txt
UPDATE:
Oops. There's an infinite loop in that code. The sequence s
matches [ tnrfv]
, so if there's a stray form feed, it'll spin forever. But [:blank:]
matches just space and tab, so the second line should be this.
{ while (/^[[:blank:]]/) {
This is a really elegant solution.
– zx485
Feb 13 at 1:26
That is very readable solution! I guess there is no point usingsed
whenawk
solution looks simpler. Thank you!
– Amomum
Feb 13 at 20:21
add a comment |
I know you said you want to use sed
, which is often a wonderful tool. But where there are choices and loops, I find that awk
outshines it.
#!/usr/bin/gawk -f
{ while (/^s/) {
if (sub(/^ /,"")) printf "<space>";
if (sub(/^t/,"")) printf "<tab>";
}
print;
}
If we create a file input.txt
containing the input example, and name the script replace
, it's run as follows, which produces the desired output.
replace input.txt
UPDATE:
Oops. There's an infinite loop in that code. The sequence s
matches [ tnrfv]
, so if there's a stray form feed, it'll spin forever. But [:blank:]
matches just space and tab, so the second line should be this.
{ while (/^[[:blank:]]/) {
This is a really elegant solution.
– zx485
Feb 13 at 1:26
That is very readable solution! I guess there is no point usingsed
whenawk
solution looks simpler. Thank you!
– Amomum
Feb 13 at 20:21
add a comment |
I know you said you want to use sed
, which is often a wonderful tool. But where there are choices and loops, I find that awk
outshines it.
#!/usr/bin/gawk -f
{ while (/^s/) {
if (sub(/^ /,"")) printf "<space>";
if (sub(/^t/,"")) printf "<tab>";
}
print;
}
If we create a file input.txt
containing the input example, and name the script replace
, it's run as follows, which produces the desired output.
replace input.txt
UPDATE:
Oops. There's an infinite loop in that code. The sequence s
matches [ tnrfv]
, so if there's a stray form feed, it'll spin forever. But [:blank:]
matches just space and tab, so the second line should be this.
{ while (/^[[:blank:]]/) {
I know you said you want to use sed
, which is often a wonderful tool. But where there are choices and loops, I find that awk
outshines it.
#!/usr/bin/gawk -f
{ while (/^s/) {
if (sub(/^ /,"")) printf "<space>";
if (sub(/^t/,"")) printf "<tab>";
}
print;
}
If we create a file input.txt
containing the input example, and name the script replace
, it's run as follows, which produces the desired output.
replace input.txt
UPDATE:
Oops. There's an infinite loop in that code. The sequence s
matches [ tnrfv]
, so if there's a stray form feed, it'll spin forever. But [:blank:]
matches just space and tab, so the second line should be this.
{ while (/^[[:blank:]]/) {
edited Feb 13 at 3:18
answered Feb 13 at 1:14
Ken JacksonKen Jackson
22112
22112
This is a really elegant solution.
– zx485
Feb 13 at 1:26
That is very readable solution! I guess there is no point usingsed
whenawk
solution looks simpler. Thank you!
– Amomum
Feb 13 at 20:21
add a comment |
This is a really elegant solution.
– zx485
Feb 13 at 1:26
That is very readable solution! I guess there is no point usingsed
whenawk
solution looks simpler. Thank you!
– Amomum
Feb 13 at 20:21
This is a really elegant solution.
– zx485
Feb 13 at 1:26
This is a really elegant solution.
– zx485
Feb 13 at 1:26
That is very readable solution! I guess there is no point using
sed
when awk
solution looks simpler. Thank you!– Amomum
Feb 13 at 20:21
That is very readable solution! I guess there is no point using
sed
when awk
solution looks simpler. Thank you!– Amomum
Feb 13 at 20:21
add a comment |
One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.
echo -e 'ttline withttabs
line with spaces
t tintermixed' | sed -r '
# On the lines that start with tab or space.
/^[t ]/ {
# Put the whole line in the hold space.
h
# Delete all tabs and spaces at the start of line.
s/^[t ]+//
# Exchange pattern and hold spaces.
# This saves the text part to the hold space and
# bring back the original line to the pattern space.
x
# Now let in pattern space only tabs and spaces
# at the start of line (the rest is on hold space).
s/^([t ]+).*/1/
# At least make the substitutions.
s/t/<TAB>/g
s/ /<SPACE>/g
# Add a n (new line) at the end of pattern space,
# then get the content of hold space and append it
# to pattern space.
G
# Delete the extra n added above.
s/n//
}'
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
add a comment |
One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.
echo -e 'ttline withttabs
line with spaces
t tintermixed' | sed -r '
# On the lines that start with tab or space.
/^[t ]/ {
# Put the whole line in the hold space.
h
# Delete all tabs and spaces at the start of line.
s/^[t ]+//
# Exchange pattern and hold spaces.
# This saves the text part to the hold space and
# bring back the original line to the pattern space.
x
# Now let in pattern space only tabs and spaces
# at the start of line (the rest is on hold space).
s/^([t ]+).*/1/
# At least make the substitutions.
s/t/<TAB>/g
s/ /<SPACE>/g
# Add a n (new line) at the end of pattern space,
# then get the content of hold space and append it
# to pattern space.
G
# Delete the extra n added above.
s/n//
}'
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
add a comment |
One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.
echo -e 'ttline withttabs
line with spaces
t tintermixed' | sed -r '
# On the lines that start with tab or space.
/^[t ]/ {
# Put the whole line in the hold space.
h
# Delete all tabs and spaces at the start of line.
s/^[t ]+//
# Exchange pattern and hold spaces.
# This saves the text part to the hold space and
# bring back the original line to the pattern space.
x
# Now let in pattern space only tabs and spaces
# at the start of line (the rest is on hold space).
s/^([t ]+).*/1/
# At least make the substitutions.
s/t/<TAB>/g
s/ /<SPACE>/g
# Add a n (new line) at the end of pattern space,
# then get the content of hold space and append it
# to pattern space.
G
# Delete the extra n added above.
s/n//
}'
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.
echo -e 'ttline withttabs
line with spaces
t tintermixed' | sed -r '
# On the lines that start with tab or space.
/^[t ]/ {
# Put the whole line in the hold space.
h
# Delete all tabs and spaces at the start of line.
s/^[t ]+//
# Exchange pattern and hold spaces.
# This saves the text part to the hold space and
# bring back the original line to the pattern space.
x
# Now let in pattern space only tabs and spaces
# at the start of line (the rest is on hold space).
s/^([t ]+).*/1/
# At least make the substitutions.
s/t/<TAB>/g
s/ /<SPACE>/g
# Add a n (new line) at the end of pattern space,
# then get the content of hold space and append it
# to pattern space.
G
# Delete the extra n added above.
s/n//
}'
<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed
answered Feb 13 at 0:41
PauloPaulo
57428
57428
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f1405022%2freplace-leading-tabs-and-spaces-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