How to replace spaces with newlines/enter in a text-file?
I have simple text file named "example".
Reading with terminal command: cat example
Output:
abc cdef ghi jk lmnopq rst uv wxyz
I want to convert (transform) into following form: (expected output from cat example)
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
How can I do this via the command-line?
(This is only an example file, I want to convert word's position in vertical-column)
command-line text-processing
add a comment |
I have simple text file named "example".
Reading with terminal command: cat example
Output:
abc cdef ghi jk lmnopq rst uv wxyz
I want to convert (transform) into following form: (expected output from cat example)
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
How can I do this via the command-line?
(This is only an example file, I want to convert word's position in vertical-column)
command-line text-processing
Related: unix.stackexchange.com/q/169995/85039
– Sergiy Kolodyazhnyy
Jan 16 '17 at 13:48
add a comment |
I have simple text file named "example".
Reading with terminal command: cat example
Output:
abc cdef ghi jk lmnopq rst uv wxyz
I want to convert (transform) into following form: (expected output from cat example)
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
How can I do this via the command-line?
(This is only an example file, I want to convert word's position in vertical-column)
command-line text-processing
I have simple text file named "example".
Reading with terminal command: cat example
Output:
abc cdef ghi jk lmnopq rst uv wxyz
I want to convert (transform) into following form: (expected output from cat example)
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
How can I do this via the command-line?
(This is only an example file, I want to convert word's position in vertical-column)
command-line text-processing
command-line text-processing
edited May 8 '14 at 10:24
Pandya
asked May 5 '14 at 14:34
PandyaPandya
20k2794157
20k2794157
Related: unix.stackexchange.com/q/169995/85039
– Sergiy Kolodyazhnyy
Jan 16 '17 at 13:48
add a comment |
Related: unix.stackexchange.com/q/169995/85039
– Sergiy Kolodyazhnyy
Jan 16 '17 at 13:48
Related: unix.stackexchange.com/q/169995/85039
– Sergiy Kolodyazhnyy
Jan 16 '17 at 13:48
Related: unix.stackexchange.com/q/169995/85039
– Sergiy Kolodyazhnyy
Jan 16 '17 at 13:48
add a comment |
6 Answers
6
active
oldest
votes
A few choices:
The classic, use
tr:
tr ' ' 'n' < example
Use
cut
cut -d ' ' --output-delimiter=$'n' -f 1- example
Use
sed
sed 's/ /n/g' example
Use
perl
perl -pe 's/ /n/g' example
Use the shell
foo=$(cat example); echo -e ${foo// /\n}
add a comment |
Try the below command
awk -v RS=" " '{print}' file
OR
awk -v RS='[n ]' '{print}' file
Example:
$ awk -v RS=" " '{print}' example
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
Explanation:
RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.
In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.
add a comment |
You can use xargs,
cat example | xargs -n 1
or, better
xargs -n 1 < example
5
xargs -n 1 < examplesaves you 1 kitten
– Rinzwind
May 5 '14 at 14:48
add a comment |
Using a perl oneliner:
perl -p -i -e 's/s/n/g' example
It will replace spaces and tabs with "ENTER" (aka n)
add a comment |
No one posted python, so here's that:
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt
We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.
add a comment |
Similar to the 'tr' above but with the additions:
Also works for tabs
Converts multiple spaces or tabs to 1 newline
tr -s '[:space:]' 'n' < example
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f461144%2fhow-to-replace-spaces-with-newlines-enter-in-a-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
A few choices:
The classic, use
tr:
tr ' ' 'n' < example
Use
cut
cut -d ' ' --output-delimiter=$'n' -f 1- example
Use
sed
sed 's/ /n/g' example
Use
perl
perl -pe 's/ /n/g' example
Use the shell
foo=$(cat example); echo -e ${foo// /\n}
add a comment |
A few choices:
The classic, use
tr:
tr ' ' 'n' < example
Use
cut
cut -d ' ' --output-delimiter=$'n' -f 1- example
Use
sed
sed 's/ /n/g' example
Use
perl
perl -pe 's/ /n/g' example
Use the shell
foo=$(cat example); echo -e ${foo// /\n}
add a comment |
A few choices:
The classic, use
tr:
tr ' ' 'n' < example
Use
cut
cut -d ' ' --output-delimiter=$'n' -f 1- example
Use
sed
sed 's/ /n/g' example
Use
perl
perl -pe 's/ /n/g' example
Use the shell
foo=$(cat example); echo -e ${foo// /\n}
A few choices:
The classic, use
tr:
tr ' ' 'n' < example
Use
cut
cut -d ' ' --output-delimiter=$'n' -f 1- example
Use
sed
sed 's/ /n/g' example
Use
perl
perl -pe 's/ /n/g' example
Use the shell
foo=$(cat example); echo -e ${foo// /\n}
edited May 6 '14 at 10:57
answered May 5 '14 at 14:42
terdon♦terdon
65.9k12138221
65.9k12138221
add a comment |
add a comment |
Try the below command
awk -v RS=" " '{print}' file
OR
awk -v RS='[n ]' '{print}' file
Example:
$ awk -v RS=" " '{print}' example
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
Explanation:
RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.
In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.
add a comment |
Try the below command
awk -v RS=" " '{print}' file
OR
awk -v RS='[n ]' '{print}' file
Example:
$ awk -v RS=" " '{print}' example
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
Explanation:
RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.
In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.
add a comment |
Try the below command
awk -v RS=" " '{print}' file
OR
awk -v RS='[n ]' '{print}' file
Example:
$ awk -v RS=" " '{print}' example
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
Explanation:
RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.
In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.
Try the below command
awk -v RS=" " '{print}' file
OR
awk -v RS='[n ]' '{print}' file
Example:
$ awk -v RS=" " '{print}' example
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
Explanation:
RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.
In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.
edited May 5 '14 at 15:00
answered May 5 '14 at 14:37
Avinash RajAvinash Raj
51.8k41168218
51.8k41168218
add a comment |
add a comment |
You can use xargs,
cat example | xargs -n 1
or, better
xargs -n 1 < example
5
xargs -n 1 < examplesaves you 1 kitten
– Rinzwind
May 5 '14 at 14:48
add a comment |
You can use xargs,
cat example | xargs -n 1
or, better
xargs -n 1 < example
5
xargs -n 1 < examplesaves you 1 kitten
– Rinzwind
May 5 '14 at 14:48
add a comment |
You can use xargs,
cat example | xargs -n 1
or, better
xargs -n 1 < example
You can use xargs,
cat example | xargs -n 1
or, better
xargs -n 1 < example
edited May 5 '14 at 14:50
answered May 5 '14 at 14:42
souravcsouravc
27k1376105
27k1376105
5
xargs -n 1 < examplesaves you 1 kitten
– Rinzwind
May 5 '14 at 14:48
add a comment |
5
xargs -n 1 < examplesaves you 1 kitten
– Rinzwind
May 5 '14 at 14:48
5
5
xargs -n 1 < example saves you 1 kitten– Rinzwind
May 5 '14 at 14:48
xargs -n 1 < example saves you 1 kitten– Rinzwind
May 5 '14 at 14:48
add a comment |
Using a perl oneliner:
perl -p -i -e 's/s/n/g' example
It will replace spaces and tabs with "ENTER" (aka n)
add a comment |
Using a perl oneliner:
perl -p -i -e 's/s/n/g' example
It will replace spaces and tabs with "ENTER" (aka n)
add a comment |
Using a perl oneliner:
perl -p -i -e 's/s/n/g' example
It will replace spaces and tabs with "ENTER" (aka n)
Using a perl oneliner:
perl -p -i -e 's/s/n/g' example
It will replace spaces and tabs with "ENTER" (aka n)
answered May 5 '14 at 14:42
Sylvain PineauSylvain Pineau
48.9k16107150
48.9k16107150
add a comment |
add a comment |
No one posted python, so here's that:
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt
We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.
add a comment |
No one posted python, so here's that:
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt
We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.
add a comment |
No one posted python, so here's that:
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt
We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.
No one posted python, so here's that:
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt
We redirect input file into python's stdin stream, and read it line by line. Each line is stripped of its trailing newline, split into words, and then rejoined into one string where each word is separated by newline.This is done to ensure having one word per line and avoid multiple newlines being inserted in case there's multiple spaces next to each other. Eventually we end up with list of strings, which is then again joined into larger string, and printed out to stdout stream. That later can be redirected to another file with > out.txt redirection.
answered Jan 16 '17 at 13:52
Sergiy KolodyazhnyySergiy Kolodyazhnyy
71.9k9148314
71.9k9148314
add a comment |
add a comment |
Similar to the 'tr' above but with the additions:
Also works for tabs
Converts multiple spaces or tabs to 1 newline
tr -s '[:space:]' 'n' < example
add a comment |
Similar to the 'tr' above but with the additions:
Also works for tabs
Converts multiple spaces or tabs to 1 newline
tr -s '[:space:]' 'n' < example
add a comment |
Similar to the 'tr' above but with the additions:
Also works for tabs
Converts multiple spaces or tabs to 1 newline
tr -s '[:space:]' 'n' < example
Similar to the 'tr' above but with the additions:
Also works for tabs
Converts multiple spaces or tabs to 1 newline
tr -s '[:space:]' 'n' < example
answered Jan 22 at 10:55
AstroTomAstroTom
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f461144%2fhow-to-replace-spaces-with-newlines-enter-in-a-text-file%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
Related: unix.stackexchange.com/q/169995/85039
– Sergiy Kolodyazhnyy
Jan 16 '17 at 13:48