How to insert newline characters every N chars into a long string
up vote
4
down vote
favorite
Using common bash tools as part of a shell script, I want to repeatedly insert a newline char ('n') into a long string at intervals of every N chars.
For example, given this string, how would I insert a newline char every 20 chars?
head -n 50 /dev/urandom | tr -dc A-Za-z0-9
Example of the results I am trying to achieve:
ZL1WEV72TTO0S83LP2I2
MTQ8DEIU3GSSYJOI9CFE
6GEPWUPCBBHLWNA4M28D
P2DHDI1L2JQIZJL0ACFV
UDYEK7HN7HQY4E2U6VFC
RH68ZZJGMSSC5YLHO0KZ
94LMELDIN1BAXQKTNSMH
0DXLM7B5966UEFGZENLZ
4917Y741L2WRTG5ACFGQ
GRVDVT3CYOLYKNT2ZYUJ
EAVN1EY4O161VTW1P3OY
Q17T24S7S9BDG1RMKGBX
WOZSI4D35U81P68NF5SB
HH7AOYHV2TWQP27A40QC
QW5N4JDK5001EAQXF41N
FKH3Q5GOQZ54HZG2FFZS
Q89KGMQZ46YBW3GVROYH
AIBOU8NFM39RYP1XBLQM
YLG8SSIW6J6XG6UJEKXO
A use-case is to quickly make a set of random passwords or ID's of a fixed length. The way I did the above example is:
for i in {1..30}; do head /dev/random | tr -dc A-Z0-9 | head -c 20 ; echo ''; done
However, for learning purposes, I want to do it a different way. I want to start with an arbitrarily long string and insert newlines, thus breaking one string into multiple small strings of fixed char length.
bash shell-script text-processing
add a comment |
up vote
4
down vote
favorite
Using common bash tools as part of a shell script, I want to repeatedly insert a newline char ('n') into a long string at intervals of every N chars.
For example, given this string, how would I insert a newline char every 20 chars?
head -n 50 /dev/urandom | tr -dc A-Za-z0-9
Example of the results I am trying to achieve:
ZL1WEV72TTO0S83LP2I2
MTQ8DEIU3GSSYJOI9CFE
6GEPWUPCBBHLWNA4M28D
P2DHDI1L2JQIZJL0ACFV
UDYEK7HN7HQY4E2U6VFC
RH68ZZJGMSSC5YLHO0KZ
94LMELDIN1BAXQKTNSMH
0DXLM7B5966UEFGZENLZ
4917Y741L2WRTG5ACFGQ
GRVDVT3CYOLYKNT2ZYUJ
EAVN1EY4O161VTW1P3OY
Q17T24S7S9BDG1RMKGBX
WOZSI4D35U81P68NF5SB
HH7AOYHV2TWQP27A40QC
QW5N4JDK5001EAQXF41N
FKH3Q5GOQZ54HZG2FFZS
Q89KGMQZ46YBW3GVROYH
AIBOU8NFM39RYP1XBLQM
YLG8SSIW6J6XG6UJEKXO
A use-case is to quickly make a set of random passwords or ID's of a fixed length. The way I did the above example is:
for i in {1..30}; do head /dev/random | tr -dc A-Z0-9 | head -c 20 ; echo ''; done
However, for learning purposes, I want to do it a different way. I want to start with an arbitrarily long string and insert newlines, thus breaking one string into multiple small strings of fixed char length.
bash shell-script text-processing
1
@thrig You should make than an answer
– xenoid
3 hours ago
fold -w 20works. I agree, you should make it an answer.
– BugBuddy
2 hours ago
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Using common bash tools as part of a shell script, I want to repeatedly insert a newline char ('n') into a long string at intervals of every N chars.
For example, given this string, how would I insert a newline char every 20 chars?
head -n 50 /dev/urandom | tr -dc A-Za-z0-9
Example of the results I am trying to achieve:
ZL1WEV72TTO0S83LP2I2
MTQ8DEIU3GSSYJOI9CFE
6GEPWUPCBBHLWNA4M28D
P2DHDI1L2JQIZJL0ACFV
UDYEK7HN7HQY4E2U6VFC
RH68ZZJGMSSC5YLHO0KZ
94LMELDIN1BAXQKTNSMH
0DXLM7B5966UEFGZENLZ
4917Y741L2WRTG5ACFGQ
GRVDVT3CYOLYKNT2ZYUJ
EAVN1EY4O161VTW1P3OY
Q17T24S7S9BDG1RMKGBX
WOZSI4D35U81P68NF5SB
HH7AOYHV2TWQP27A40QC
QW5N4JDK5001EAQXF41N
FKH3Q5GOQZ54HZG2FFZS
Q89KGMQZ46YBW3GVROYH
AIBOU8NFM39RYP1XBLQM
YLG8SSIW6J6XG6UJEKXO
A use-case is to quickly make a set of random passwords or ID's of a fixed length. The way I did the above example is:
for i in {1..30}; do head /dev/random | tr -dc A-Z0-9 | head -c 20 ; echo ''; done
However, for learning purposes, I want to do it a different way. I want to start with an arbitrarily long string and insert newlines, thus breaking one string into multiple small strings of fixed char length.
bash shell-script text-processing
Using common bash tools as part of a shell script, I want to repeatedly insert a newline char ('n') into a long string at intervals of every N chars.
For example, given this string, how would I insert a newline char every 20 chars?
head -n 50 /dev/urandom | tr -dc A-Za-z0-9
Example of the results I am trying to achieve:
ZL1WEV72TTO0S83LP2I2
MTQ8DEIU3GSSYJOI9CFE
6GEPWUPCBBHLWNA4M28D
P2DHDI1L2JQIZJL0ACFV
UDYEK7HN7HQY4E2U6VFC
RH68ZZJGMSSC5YLHO0KZ
94LMELDIN1BAXQKTNSMH
0DXLM7B5966UEFGZENLZ
4917Y741L2WRTG5ACFGQ
GRVDVT3CYOLYKNT2ZYUJ
EAVN1EY4O161VTW1P3OY
Q17T24S7S9BDG1RMKGBX
WOZSI4D35U81P68NF5SB
HH7AOYHV2TWQP27A40QC
QW5N4JDK5001EAQXF41N
FKH3Q5GOQZ54HZG2FFZS
Q89KGMQZ46YBW3GVROYH
AIBOU8NFM39RYP1XBLQM
YLG8SSIW6J6XG6UJEKXO
A use-case is to quickly make a set of random passwords or ID's of a fixed length. The way I did the above example is:
for i in {1..30}; do head /dev/random | tr -dc A-Z0-9 | head -c 20 ; echo ''; done
However, for learning purposes, I want to do it a different way. I want to start with an arbitrarily long string and insert newlines, thus breaking one string into multiple small strings of fixed char length.
bash shell-script text-processing
bash shell-script text-processing
asked 4 hours ago
BugBuddy
1057
1057
1
@thrig You should make than an answer
– xenoid
3 hours ago
fold -w 20works. I agree, you should make it an answer.
– BugBuddy
2 hours ago
add a comment |
1
@thrig You should make than an answer
– xenoid
3 hours ago
fold -w 20works. I agree, you should make it an answer.
– BugBuddy
2 hours ago
1
1
@thrig You should make than an answer
– xenoid
3 hours ago
@thrig You should make than an answer
– xenoid
3 hours ago
fold -w 20 works. I agree, you should make it an answer.– BugBuddy
2 hours ago
fold -w 20 works. I agree, you should make it an answer.– BugBuddy
2 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
I like the fold answer but just in case you want with sed, here it is:
sed 's/.{20}/&
/g' filename
You can use -i for in-place insertion.
thanks. I voted your answer up, but I like thefoldsolution too.
– BugBuddy
1 hour ago
add a comment |
up vote
3
down vote
The venerable fold command ("written by Bill Joy on June 28, 1977") can wrap lines:
$ printf "foobarzotn" | fold -w 3
foo
bar
zot
However, there are some edge cases
BUGS
Traditional roff(7) output semantics, implemented both by GNU nroff and
by mandoc(1), only uses a single backspace for backing up the previous
character, even for double-width characters. The fold backspace
semantics required by POSIX mishandles such backspace-encoded sequences,
breaking lines early. The fmt(1) utility provides similar functionality
and does not suffer from that problem, but isn't standardized by POSIX.
so if your input has backspace characters you may need to filter or remove those
$ printf "abcbdben" | col -b | fold -w 1
e
$ printf "abcbdben" | tr -d "b" | fold -w 1
a
c
d
e
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%2f489775%2fhow-to-insert-newline-characters-every-n-chars-into-a-long-string%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
up vote
3
down vote
I like the fold answer but just in case you want with sed, here it is:
sed 's/.{20}/&
/g' filename
You can use -i for in-place insertion.
thanks. I voted your answer up, but I like thefoldsolution too.
– BugBuddy
1 hour ago
add a comment |
up vote
3
down vote
I like the fold answer but just in case you want with sed, here it is:
sed 's/.{20}/&
/g' filename
You can use -i for in-place insertion.
thanks. I voted your answer up, but I like thefoldsolution too.
– BugBuddy
1 hour ago
add a comment |
up vote
3
down vote
up vote
3
down vote
I like the fold answer but just in case you want with sed, here it is:
sed 's/.{20}/&
/g' filename
You can use -i for in-place insertion.
I like the fold answer but just in case you want with sed, here it is:
sed 's/.{20}/&
/g' filename
You can use -i for in-place insertion.
answered 1 hour ago
unxnut
3,5922918
3,5922918
thanks. I voted your answer up, but I like thefoldsolution too.
– BugBuddy
1 hour ago
add a comment |
thanks. I voted your answer up, but I like thefoldsolution too.
– BugBuddy
1 hour ago
thanks. I voted your answer up, but I like the
fold solution too.– BugBuddy
1 hour ago
thanks. I voted your answer up, but I like the
fold solution too.– BugBuddy
1 hour ago
add a comment |
up vote
3
down vote
The venerable fold command ("written by Bill Joy on June 28, 1977") can wrap lines:
$ printf "foobarzotn" | fold -w 3
foo
bar
zot
However, there are some edge cases
BUGS
Traditional roff(7) output semantics, implemented both by GNU nroff and
by mandoc(1), only uses a single backspace for backing up the previous
character, even for double-width characters. The fold backspace
semantics required by POSIX mishandles such backspace-encoded sequences,
breaking lines early. The fmt(1) utility provides similar functionality
and does not suffer from that problem, but isn't standardized by POSIX.
so if your input has backspace characters you may need to filter or remove those
$ printf "abcbdben" | col -b | fold -w 1
e
$ printf "abcbdben" | tr -d "b" | fold -w 1
a
c
d
e
add a comment |
up vote
3
down vote
The venerable fold command ("written by Bill Joy on June 28, 1977") can wrap lines:
$ printf "foobarzotn" | fold -w 3
foo
bar
zot
However, there are some edge cases
BUGS
Traditional roff(7) output semantics, implemented both by GNU nroff and
by mandoc(1), only uses a single backspace for backing up the previous
character, even for double-width characters. The fold backspace
semantics required by POSIX mishandles such backspace-encoded sequences,
breaking lines early. The fmt(1) utility provides similar functionality
and does not suffer from that problem, but isn't standardized by POSIX.
so if your input has backspace characters you may need to filter or remove those
$ printf "abcbdben" | col -b | fold -w 1
e
$ printf "abcbdben" | tr -d "b" | fold -w 1
a
c
d
e
add a comment |
up vote
3
down vote
up vote
3
down vote
The venerable fold command ("written by Bill Joy on June 28, 1977") can wrap lines:
$ printf "foobarzotn" | fold -w 3
foo
bar
zot
However, there are some edge cases
BUGS
Traditional roff(7) output semantics, implemented both by GNU nroff and
by mandoc(1), only uses a single backspace for backing up the previous
character, even for double-width characters. The fold backspace
semantics required by POSIX mishandles such backspace-encoded sequences,
breaking lines early. The fmt(1) utility provides similar functionality
and does not suffer from that problem, but isn't standardized by POSIX.
so if your input has backspace characters you may need to filter or remove those
$ printf "abcbdben" | col -b | fold -w 1
e
$ printf "abcbdben" | tr -d "b" | fold -w 1
a
c
d
e
The venerable fold command ("written by Bill Joy on June 28, 1977") can wrap lines:
$ printf "foobarzotn" | fold -w 3
foo
bar
zot
However, there are some edge cases
BUGS
Traditional roff(7) output semantics, implemented both by GNU nroff and
by mandoc(1), only uses a single backspace for backing up the previous
character, even for double-width characters. The fold backspace
semantics required by POSIX mishandles such backspace-encoded sequences,
breaking lines early. The fmt(1) utility provides similar functionality
and does not suffer from that problem, but isn't standardized by POSIX.
so if your input has backspace characters you may need to filter or remove those
$ printf "abcbdben" | col -b | fold -w 1
e
$ printf "abcbdben" | tr -d "b" | fold -w 1
a
c
d
e
answered 1 hour ago
thrig
24k22955
24k22955
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%2f489775%2fhow-to-insert-newline-characters-every-n-chars-into-a-long-string%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
1
@thrig You should make than an answer
– xenoid
3 hours ago
fold -w 20works. I agree, you should make it an answer.– BugBuddy
2 hours ago