How to convert rows into space-delimited line and vice versa

Last updated on September 20, 2020 by Dan Nanni

As part of text-file batching processing job, if you would like to convert rows into a space-delimited line, or replace spaces with line break (or newline), you can use command line utilities such as tr and awk. The tr command can perform simple character translation or deletion on input text file, while the awk utility can perform more powerful pattern scanning and processing on input file. So these tools can be quite useful in any kind of shell scripts.

To convert rows into a space-delimited line:
$ cat intput.txt
apple
mango
watermelon
orange
pineapple
kiwi
$ cat input.txt | tr "n" " "
apple mango watermelon orange pineapple kiwi
To convert spaces to line break (newline):
$ cat intput2.txt
january february march april may
sed -e 's/\s\+/\n/g' input2.txt
january
february
march
april
may

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean