Friday, 14 September 2012

Replace spaces with newlines using terminal

problem:

Using Linux or OS X, I would like to replace spaces with newline characters in a file and output the result to a new file.

For instance, if my file's contents look like the following:
Monday Tuesday Wednesday Thursday Friday

The output should look like:
Monday
Tuesday
Wednesday
Thursday
Friday

solution:

In the terminal, a simple solution would be to use the 'tr' command, for instance:
cat infile.txt | xargs | tr -s ' ' '\n' > outfile.txt

reference:

http://www.linfo.org/tr.html

No comments:

Post a Comment