How to words in arrange as Table

Hi Team,

I have one file in that almost 100+ words in lines

Eg:-

Unix
windows
solaris
Linux
...
...

But I want arrange all lines in table format and can able read on screen

Eg: -

Unix         windows    solaris   Lunix......
Hp unix     Mac-os ......

Like as Table... Please help me how to do ( I think sed or awk is good... no idea)

Thanks for your help.

Try paste with the appropriate number of delimiters, e.g. -d ' \n'

HI Rudic,

Still I can see only lines after use

-d '\n'

, but, If use -s I am serial lines with only once character space.

$cat tmp.txt
unix
windows
solaris
MacOS
$
 
 
 
$ paste -d '\n' tmp.txt
unix
windows
solaris
MacOS
$
 
-s Gives serial words with single space 
$ paste -s tmp.txt
unix    windows solaris MacOS
$

Joins into ten columns per record:

awk '
    {
        if( c > 10 )        # 10 columns; customise as needed
        {
            printf( "\n" );
            c = 0;
        }
        printf( "%s%s", c++ ? "\t" : "", $1 );
    }
    END { printf( "\n" ); }
' input-file
sed 's/.*/"&"/' file | xargs -n4 printf '%-15s %-15s %-15s %-15s\n'

Regards,
Alister

You're almost there. Make it -d' \n' or -d'\t\t\t\n', i.e. as many delimiters (space or TAB or linefeed) as you want columns, and you win!

Let me know if this did not meet your requirement...

 awk '{ORS="\t";}1' file_name

And if you want to limit the columns...

awk '{if(NR%4==0){ORS="\n"} else {ORS="\t"}}1' file_name 

change the above boldened number to set number of columns to whatever you desire...

Thanks Alister,
His script works beautifully

 
 $ sed 's/.*/"&"/' tmp.txt | xargs -n4 printf '%-15s %-15s %-15s %-15s\n'
unix            windows         solaris         MacOS
 $ 

Hi Agama: I test your script and below errors

 
awk: syntax error near line 8
awk: illegal statement near line 8

might be there is error in this line

 
printf( "%s%s", c++ ? "\t" : "", $1 );

Hi Rudic, I have tested again but the same out

$ paste -d"\t\t\t\n" tmp.txt
unix
windows
solaris
MacOS
 $ 

Hi Msabhi, I have also tested your script and below error

 
 $ awk '{if(NR%5==0){ORS="\n"} else {ORS="\t"}}1' tmp.txt
awk: syntax error near line 1
awk: bailing out near line 1
 $ 

Thanks for all to comments here.

No errors generated when I run it (gawk 4.0). What version of awk are you using? If you're on Solaris, try nawk.

I also tried Msabhi's code; again without errors, so can only imagine you are using an old (ancient?) version of awk.

You'd also require -s to achieve your intended effect.

Regards,
Alister

@Alister: Agree. My fault, thought about it but did not put in post. Thanks for clarification

Hi Agama, Yes I am using solaris 5.10 version. I am sorry mentioned OS version here.