Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print one word per line

Tags:

unix

ksh

sed

awk

I tried almost everything, sed, awk, tr, but...

I'm trying to output a file containing this

 2079 May 19 13:37 temp.sh
 1024 May 23 17:09 mirrad
 478 May 26 14:48 unzip.sh

like this

 2079
 May
 19
 13:37
 .
 .
 .

So each string will be printed out one at the time in a variable.

like image 641
Driven Avatar asked Jun 15 '16 17:06

Driven


Video Answer


1 Answers

Another simple one liner using xargs

xargs -n 1 <file

where -n explanation from man page:-

-n max-args, --max-args=max-args
       Use at most max-args arguments per command line.  Fewer than
       max-args arguments will be used if the size (see the -s
       option) is exceeded, unless the -x option is given, in which
       case xargs will exit.

will produce output as

#!/bin/bash
$ xargs -n1 <tmp.txt
2079
May
19
13:37
temp.sh
1024
May
23
17:09
mirrad

with -n value as 2 it gives

#!/bin/bash     
$ xargs -n2 <tmp.txt
2079 May
19 13:37
temp.sh 1024
May 23
17:09 mirrad

Note

The suggestion with xargs only works for a simple input as shown in OP. As such the solution doesn't work for huge files (needing high in memory processing) or in files containing lines with quotes. See other answers involving awk for the same.

like image 192
Inian Avatar answered Sep 28 '22 11:09

Inian