How to avoid printing a newline when seq completes?
To create a column header, looking like:
1234567890123456789
I (am trying to) use seq and echo:
seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9
However, seq outputs a newline after each run. How can I avoid that?
seq
add a comment |
To create a column header, looking like:
1234567890123456789
I (am trying to) use seq and echo:
seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9
However, seq outputs a newline after each run. How can I avoid that?
seq
Fair enough. Sorry about that, it seemed more natural to me.
– terdon♦
32 mins ago
add a comment |
To create a column header, looking like:
1234567890123456789
I (am trying to) use seq and echo:
seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9
However, seq outputs a newline after each run. How can I avoid that?
seq
To create a column header, looking like:
1234567890123456789
I (am trying to) use seq and echo:
seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9
However, seq outputs a newline after each run. How can I avoid that?
seq
seq
edited 36 mins ago
asked 59 mins ago
GreenMatt
20617
20617
Fair enough. Sorry about that, it seemed more natural to me.
– terdon♦
32 mins ago
add a comment |
Fair enough. Sorry about that, it seemed more natural to me.
– terdon♦
32 mins ago
Fair enough. Sorry about that, it seemed more natural to me.
– terdon♦
32 mins ago
Fair enough. Sorry about that, it seemed more natural to me.
– terdon♦
32 mins ago
add a comment |
4 Answers
4
active
oldest
votes
Assuming you just want to print 1234567890123456789, you can do it with:
$ printf "%s" $(seq 1 9) $(seq 0 9)
1234567890123456789$
That won't have a trailing newline at all though, so maybe you prefer:
$ printf "%s" $(seq 1 9) $(seq 0 9) $'n'
1234567890123456789
$
A few simpler choices if you don't need to use seq:
$ perl -le 'print 1..9,0,1..9'
1234567890123456789
$ printf "%s" {1..9} {0..9} $'n'
1234567890123456789
Since you mentioned portability, I recommend you use the perl approach, or if you are likely to encounter systems without perl, and yet need the same command to run in shells including bash, sh, dash, tcsh etc, try Kamil's approach.
Thanks. How portable is that? It worked under Bash. Under tcsh I got an "Illegal variable name" error. Under sh, the 'n' was displayed.
– GreenMatt
37 mins ago
1
@GreenMatt ah, that depends. For extreme portability, you may as well just use theperlone which is shell-agnostic. Theprintf "%s" $(seq 1 9) $(seq 0 9)will work in any POSIX shell, includingshanddash, but theprintf "%s" $(seq 1 9) $(seq 0 9) $'n'will fail indash. Note that it does work insh(or at least inbashrunning assh, so in POSIX mode), I assume you're running Ubuntu or a simmilar system whoseshis actuallydash.tcshis a strange one and not even trying to be POSIX so don't expect any of the shell-based approaches to work there.
– terdon♦
33 mins ago
1
@GreenMatt of course, if you need portability, you should also avoid usingecho.
– terdon♦
29 mins ago
Perl? <shudder>
– GreenMatt
16 mins ago
add a comment |
You can remove newlines from any stream with tr -d 'n'. In your case
(seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9) | tr -d 'n'
While other answers may concentrate on modifying your original approach, this is the way that should just remove newline characters regardless of what is before |.
1
Nice. This also has the benefit of being portable to most (all?) shells. I think the OP wants the final newline, so another option might be( seq -s '' 1 9 ; echo -n 0 ; seq -s '' 1 9 ) | tr -d 'n' ; printf "n".
– terdon♦
20 mins ago
Ah yes, I never think oftr.
– GreenMatt
16 mins ago
add a comment |
Consider also:
printf '%d' $(seq -w 1 1 99 | cut -c2)
We generate the numbers 01..99, zero-padded (-w), then strip off the tens-place. Those ones-place numbers are then sent to printf to be printed individually.
To get your desired output, use 20 instead:
$ printf '%d' $(seq -w 1 1 20 | cut -c2)
12345678901234567890
add a comment |
I would like to reinforce the answer of @terdon, but propose a solution that works with the tcsh shell as well.
Unfortunately, I didn't have enough reputation to write this as a comment:
printf "%sn" "seq 1 9` `seq 0 9`"`
The backtick - like all methods of command-line substitution -, should cut the trailing newline. Then, printf's own backslash-escape interpretation is used, instead of the ANSI string expansion used in terdon's answer.
It also works like this, by allowing printf to interpret all escape sequences after substitution with the format specifiers (%b specifier):
printf "%b" "seq 1 9` `seq 0 9`n"`
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%2f492315%2fhow-to-avoid-printing-a-newline-when-seq-completes%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
Assuming you just want to print 1234567890123456789, you can do it with:
$ printf "%s" $(seq 1 9) $(seq 0 9)
1234567890123456789$
That won't have a trailing newline at all though, so maybe you prefer:
$ printf "%s" $(seq 1 9) $(seq 0 9) $'n'
1234567890123456789
$
A few simpler choices if you don't need to use seq:
$ perl -le 'print 1..9,0,1..9'
1234567890123456789
$ printf "%s" {1..9} {0..9} $'n'
1234567890123456789
Since you mentioned portability, I recommend you use the perl approach, or if you are likely to encounter systems without perl, and yet need the same command to run in shells including bash, sh, dash, tcsh etc, try Kamil's approach.
Thanks. How portable is that? It worked under Bash. Under tcsh I got an "Illegal variable name" error. Under sh, the 'n' was displayed.
– GreenMatt
37 mins ago
1
@GreenMatt ah, that depends. For extreme portability, you may as well just use theperlone which is shell-agnostic. Theprintf "%s" $(seq 1 9) $(seq 0 9)will work in any POSIX shell, includingshanddash, but theprintf "%s" $(seq 1 9) $(seq 0 9) $'n'will fail indash. Note that it does work insh(or at least inbashrunning assh, so in POSIX mode), I assume you're running Ubuntu or a simmilar system whoseshis actuallydash.tcshis a strange one and not even trying to be POSIX so don't expect any of the shell-based approaches to work there.
– terdon♦
33 mins ago
1
@GreenMatt of course, if you need portability, you should also avoid usingecho.
– terdon♦
29 mins ago
Perl? <shudder>
– GreenMatt
16 mins ago
add a comment |
Assuming you just want to print 1234567890123456789, you can do it with:
$ printf "%s" $(seq 1 9) $(seq 0 9)
1234567890123456789$
That won't have a trailing newline at all though, so maybe you prefer:
$ printf "%s" $(seq 1 9) $(seq 0 9) $'n'
1234567890123456789
$
A few simpler choices if you don't need to use seq:
$ perl -le 'print 1..9,0,1..9'
1234567890123456789
$ printf "%s" {1..9} {0..9} $'n'
1234567890123456789
Since you mentioned portability, I recommend you use the perl approach, or if you are likely to encounter systems without perl, and yet need the same command to run in shells including bash, sh, dash, tcsh etc, try Kamil's approach.
Thanks. How portable is that? It worked under Bash. Under tcsh I got an "Illegal variable name" error. Under sh, the 'n' was displayed.
– GreenMatt
37 mins ago
1
@GreenMatt ah, that depends. For extreme portability, you may as well just use theperlone which is shell-agnostic. Theprintf "%s" $(seq 1 9) $(seq 0 9)will work in any POSIX shell, includingshanddash, but theprintf "%s" $(seq 1 9) $(seq 0 9) $'n'will fail indash. Note that it does work insh(or at least inbashrunning assh, so in POSIX mode), I assume you're running Ubuntu or a simmilar system whoseshis actuallydash.tcshis a strange one and not even trying to be POSIX so don't expect any of the shell-based approaches to work there.
– terdon♦
33 mins ago
1
@GreenMatt of course, if you need portability, you should also avoid usingecho.
– terdon♦
29 mins ago
Perl? <shudder>
– GreenMatt
16 mins ago
add a comment |
Assuming you just want to print 1234567890123456789, you can do it with:
$ printf "%s" $(seq 1 9) $(seq 0 9)
1234567890123456789$
That won't have a trailing newline at all though, so maybe you prefer:
$ printf "%s" $(seq 1 9) $(seq 0 9) $'n'
1234567890123456789
$
A few simpler choices if you don't need to use seq:
$ perl -le 'print 1..9,0,1..9'
1234567890123456789
$ printf "%s" {1..9} {0..9} $'n'
1234567890123456789
Since you mentioned portability, I recommend you use the perl approach, or if you are likely to encounter systems without perl, and yet need the same command to run in shells including bash, sh, dash, tcsh etc, try Kamil's approach.
Assuming you just want to print 1234567890123456789, you can do it with:
$ printf "%s" $(seq 1 9) $(seq 0 9)
1234567890123456789$
That won't have a trailing newline at all though, so maybe you prefer:
$ printf "%s" $(seq 1 9) $(seq 0 9) $'n'
1234567890123456789
$
A few simpler choices if you don't need to use seq:
$ perl -le 'print 1..9,0,1..9'
1234567890123456789
$ printf "%s" {1..9} {0..9} $'n'
1234567890123456789
Since you mentioned portability, I recommend you use the perl approach, or if you are likely to encounter systems without perl, and yet need the same command to run in shells including bash, sh, dash, tcsh etc, try Kamil's approach.
edited 19 mins ago
answered 54 mins ago
terdon♦
128k31249423
128k31249423
Thanks. How portable is that? It worked under Bash. Under tcsh I got an "Illegal variable name" error. Under sh, the 'n' was displayed.
– GreenMatt
37 mins ago
1
@GreenMatt ah, that depends. For extreme portability, you may as well just use theperlone which is shell-agnostic. Theprintf "%s" $(seq 1 9) $(seq 0 9)will work in any POSIX shell, includingshanddash, but theprintf "%s" $(seq 1 9) $(seq 0 9) $'n'will fail indash. Note that it does work insh(or at least inbashrunning assh, so in POSIX mode), I assume you're running Ubuntu or a simmilar system whoseshis actuallydash.tcshis a strange one and not even trying to be POSIX so don't expect any of the shell-based approaches to work there.
– terdon♦
33 mins ago
1
@GreenMatt of course, if you need portability, you should also avoid usingecho.
– terdon♦
29 mins ago
Perl? <shudder>
– GreenMatt
16 mins ago
add a comment |
Thanks. How portable is that? It worked under Bash. Under tcsh I got an "Illegal variable name" error. Under sh, the 'n' was displayed.
– GreenMatt
37 mins ago
1
@GreenMatt ah, that depends. For extreme portability, you may as well just use theperlone which is shell-agnostic. Theprintf "%s" $(seq 1 9) $(seq 0 9)will work in any POSIX shell, includingshanddash, but theprintf "%s" $(seq 1 9) $(seq 0 9) $'n'will fail indash. Note that it does work insh(or at least inbashrunning assh, so in POSIX mode), I assume you're running Ubuntu or a simmilar system whoseshis actuallydash.tcshis a strange one and not even trying to be POSIX so don't expect any of the shell-based approaches to work there.
– terdon♦
33 mins ago
1
@GreenMatt of course, if you need portability, you should also avoid usingecho.
– terdon♦
29 mins ago
Perl? <shudder>
– GreenMatt
16 mins ago
Thanks. How portable is that? It worked under Bash. Under tcsh I got an "Illegal variable name" error. Under sh, the 'n' was displayed.
– GreenMatt
37 mins ago
Thanks. How portable is that? It worked under Bash. Under tcsh I got an "Illegal variable name" error. Under sh, the 'n' was displayed.
– GreenMatt
37 mins ago
1
1
@GreenMatt ah, that depends. For extreme portability, you may as well just use the
perl one which is shell-agnostic. The printf "%s" $(seq 1 9) $(seq 0 9) will work in any POSIX shell, including sh and dash, but the printf "%s" $(seq 1 9) $(seq 0 9) $'n' will fail in dash. Note that it does work in sh (or at least in bash running as sh, so in POSIX mode), I assume you're running Ubuntu or a simmilar system whose sh is actually dash. tcsh is a strange one and not even trying to be POSIX so don't expect any of the shell-based approaches to work there.– terdon♦
33 mins ago
@GreenMatt ah, that depends. For extreme portability, you may as well just use the
perl one which is shell-agnostic. The printf "%s" $(seq 1 9) $(seq 0 9) will work in any POSIX shell, including sh and dash, but the printf "%s" $(seq 1 9) $(seq 0 9) $'n' will fail in dash. Note that it does work in sh (or at least in bash running as sh, so in POSIX mode), I assume you're running Ubuntu or a simmilar system whose sh is actually dash. tcsh is a strange one and not even trying to be POSIX so don't expect any of the shell-based approaches to work there.– terdon♦
33 mins ago
1
1
@GreenMatt of course, if you need portability, you should also avoid using
echo.– terdon♦
29 mins ago
@GreenMatt of course, if you need portability, you should also avoid using
echo.– terdon♦
29 mins ago
Perl? <shudder>
– GreenMatt
16 mins ago
Perl? <shudder>
– GreenMatt
16 mins ago
add a comment |
You can remove newlines from any stream with tr -d 'n'. In your case
(seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9) | tr -d 'n'
While other answers may concentrate on modifying your original approach, this is the way that should just remove newline characters regardless of what is before |.
1
Nice. This also has the benefit of being portable to most (all?) shells. I think the OP wants the final newline, so another option might be( seq -s '' 1 9 ; echo -n 0 ; seq -s '' 1 9 ) | tr -d 'n' ; printf "n".
– terdon♦
20 mins ago
Ah yes, I never think oftr.
– GreenMatt
16 mins ago
add a comment |
You can remove newlines from any stream with tr -d 'n'. In your case
(seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9) | tr -d 'n'
While other answers may concentrate on modifying your original approach, this is the way that should just remove newline characters regardless of what is before |.
1
Nice. This also has the benefit of being portable to most (all?) shells. I think the OP wants the final newline, so another option might be( seq -s '' 1 9 ; echo -n 0 ; seq -s '' 1 9 ) | tr -d 'n' ; printf "n".
– terdon♦
20 mins ago
Ah yes, I never think oftr.
– GreenMatt
16 mins ago
add a comment |
You can remove newlines from any stream with tr -d 'n'. In your case
(seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9) | tr -d 'n'
While other answers may concentrate on modifying your original approach, this is the way that should just remove newline characters regardless of what is before |.
You can remove newlines from any stream with tr -d 'n'. In your case
(seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9) | tr -d 'n'
While other answers may concentrate on modifying your original approach, this is the way that should just remove newline characters regardless of what is before |.
answered 31 mins ago
Kamil Maciorowski
1,2191625
1,2191625
1
Nice. This also has the benefit of being portable to most (all?) shells. I think the OP wants the final newline, so another option might be( seq -s '' 1 9 ; echo -n 0 ; seq -s '' 1 9 ) | tr -d 'n' ; printf "n".
– terdon♦
20 mins ago
Ah yes, I never think oftr.
– GreenMatt
16 mins ago
add a comment |
1
Nice. This also has the benefit of being portable to most (all?) shells. I think the OP wants the final newline, so another option might be( seq -s '' 1 9 ; echo -n 0 ; seq -s '' 1 9 ) | tr -d 'n' ; printf "n".
– terdon♦
20 mins ago
Ah yes, I never think oftr.
– GreenMatt
16 mins ago
1
1
Nice. This also has the benefit of being portable to most (all?) shells. I think the OP wants the final newline, so another option might be
( seq -s '' 1 9 ; echo -n 0 ; seq -s '' 1 9 ) | tr -d 'n' ; printf "n".– terdon♦
20 mins ago
Nice. This also has the benefit of being portable to most (all?) shells. I think the OP wants the final newline, so another option might be
( seq -s '' 1 9 ; echo -n 0 ; seq -s '' 1 9 ) | tr -d 'n' ; printf "n".– terdon♦
20 mins ago
Ah yes, I never think of
tr.– GreenMatt
16 mins ago
Ah yes, I never think of
tr.– GreenMatt
16 mins ago
add a comment |
Consider also:
printf '%d' $(seq -w 1 1 99 | cut -c2)
We generate the numbers 01..99, zero-padded (-w), then strip off the tens-place. Those ones-place numbers are then sent to printf to be printed individually.
To get your desired output, use 20 instead:
$ printf '%d' $(seq -w 1 1 20 | cut -c2)
12345678901234567890
add a comment |
Consider also:
printf '%d' $(seq -w 1 1 99 | cut -c2)
We generate the numbers 01..99, zero-padded (-w), then strip off the tens-place. Those ones-place numbers are then sent to printf to be printed individually.
To get your desired output, use 20 instead:
$ printf '%d' $(seq -w 1 1 20 | cut -c2)
12345678901234567890
add a comment |
Consider also:
printf '%d' $(seq -w 1 1 99 | cut -c2)
We generate the numbers 01..99, zero-padded (-w), then strip off the tens-place. Those ones-place numbers are then sent to printf to be printed individually.
To get your desired output, use 20 instead:
$ printf '%d' $(seq -w 1 1 20 | cut -c2)
12345678901234567890
Consider also:
printf '%d' $(seq -w 1 1 99 | cut -c2)
We generate the numbers 01..99, zero-padded (-w), then strip off the tens-place. Those ones-place numbers are then sent to printf to be printed individually.
To get your desired output, use 20 instead:
$ printf '%d' $(seq -w 1 1 20 | cut -c2)
12345678901234567890
edited 38 mins ago
terdon♦
128k31249423
128k31249423
answered 45 mins ago
Jeff Schaller
38.8k1053125
38.8k1053125
add a comment |
add a comment |
I would like to reinforce the answer of @terdon, but propose a solution that works with the tcsh shell as well.
Unfortunately, I didn't have enough reputation to write this as a comment:
printf "%sn" "seq 1 9` `seq 0 9`"`
The backtick - like all methods of command-line substitution -, should cut the trailing newline. Then, printf's own backslash-escape interpretation is used, instead of the ANSI string expansion used in terdon's answer.
It also works like this, by allowing printf to interpret all escape sequences after substitution with the format specifiers (%b specifier):
printf "%b" "seq 1 9` `seq 0 9`n"`
add a comment |
I would like to reinforce the answer of @terdon, but propose a solution that works with the tcsh shell as well.
Unfortunately, I didn't have enough reputation to write this as a comment:
printf "%sn" "seq 1 9` `seq 0 9`"`
The backtick - like all methods of command-line substitution -, should cut the trailing newline. Then, printf's own backslash-escape interpretation is used, instead of the ANSI string expansion used in terdon's answer.
It also works like this, by allowing printf to interpret all escape sequences after substitution with the format specifiers (%b specifier):
printf "%b" "seq 1 9` `seq 0 9`n"`
add a comment |
I would like to reinforce the answer of @terdon, but propose a solution that works with the tcsh shell as well.
Unfortunately, I didn't have enough reputation to write this as a comment:
printf "%sn" "seq 1 9` `seq 0 9`"`
The backtick - like all methods of command-line substitution -, should cut the trailing newline. Then, printf's own backslash-escape interpretation is used, instead of the ANSI string expansion used in terdon's answer.
It also works like this, by allowing printf to interpret all escape sequences after substitution with the format specifiers (%b specifier):
printf "%b" "seq 1 9` `seq 0 9`n"`
I would like to reinforce the answer of @terdon, but propose a solution that works with the tcsh shell as well.
Unfortunately, I didn't have enough reputation to write this as a comment:
printf "%sn" "seq 1 9` `seq 0 9`"`
The backtick - like all methods of command-line substitution -, should cut the trailing newline. Then, printf's own backslash-escape interpretation is used, instead of the ANSI string expansion used in terdon's answer.
It also works like this, by allowing printf to interpret all escape sequences after substitution with the format specifiers (%b specifier):
printf "%b" "seq 1 9` `seq 0 9`n"`
answered 16 mins ago
Larry
463
463
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f492315%2fhow-to-avoid-printing-a-newline-when-seq-completes%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
Fair enough. Sorry about that, it seemed more natural to me.
– terdon♦
32 mins ago