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)
A few choices:
The classic, use
tr
:Use
cut
Use
sed
Use
perl
Use the shell
Try the below command
OR
Example:
Explanation:
RS (Record separator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. 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.
You can use
xargs
,or, better
Using a
perl
oneliner:It will replace spaces and tabs with "ENTER" (aka
\n
)No one posted python, so here's that:
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.Similar to the 'tr' above but with the additions:
Also works for tabs
Converts multiple spaces or tabs to 1 newline