While dealing with text processing jobs we would like to convert a selection of rows separated by \n and convert it to space delimited single line. 
We will do this with ‘tr’ – unix tool that translates, deletes, squeezes characters from standard input and writes the same to standard output. “Awk” is a powerful tool used to perform pattern scannind and processing language and it is one of the important commands, a powerful linux user should be aware of its usage.

 

To convert rows into a space-delimited line and save to a file:

$ cat intput.txt

ankit
shah
is
my
name
I
am
awesome

$ cat input.txt | tr “\n” ” ” | tee output.log

ankit shah is my name I am awesome

To convert spaces to line break (newline) and save to a file:

$ cat intput.txt

Hi how are you doing

sed -e ‘s/\s\+/\n/g’ input.txt | tee output.txt

Hi
how
are
you
doing

Advertisements