Want to format output.

Hi,

I have some data in text file as

123
45
34
5
66

I want to create a file as in table format as below:

 
table1          123       45        5
tab2            66        34
tabllllll       123       23

i tried

ifs="$IFS"
IFS='
'
set -A lines $(<ino3.txt)
IFS="$ifs"

 
 
echo TAble1 ${lines[0]}          ${lines[1]}           ${lines[2]}
echo Tbl2   ${lines[3]}          ${lines[1]}           ${lines[2]}
 

but this is not giving properly and getting jumbled as below:

TAble1 490 7127 399
Tbl2 0 7127 399

Please help me to format this.

Provide a better example of your input file and the desired output.

Regards

Thanks for response...!!!

Input file:

897
897
8642
8642
888
888
25597
25597
7798
7798
461
461
29
29
30522
10356
462

And Desired Output would be:

TABLE NAME                   COUNT1            COUNT2       
----------------------------------------------------
table 1                      897                  897
tableName2                   8642                 8642
table name                   888                  888
SomeName                     25997                25997

and so on....!!!

The Name of tables are known so if required can be saved in separate text file or can b defined in variable...!!!

I hope i am able to xplain things, please do ask if any other information is required.

try if this is ok...

i=0
while read field
do
   i=$(($i+1))
   arr_field[$i]=$field
done < FILE_WITH_NUMBERS
j=1
column1=1
while [ $j -le $i ]
do
   if [ $column1 -eq 1 ]
   then
      echo "table$j\t${arr_field[$j]}\c"
      column1=0
   else
      echo "\t${arr_field[$j]}"
      column1=1
   fi
   j=$(($j+1))
done
echo
$ cat infile
897
897
8642
8642
888
888
25597
25597
7798
7798
461
461

$ cat tables
table 1
tableName2
table name
SomeName
Anothername
Yet another

Now the script

#!/bin/bash
FORMAT='%-20s%8s%8s\n'
printf "$FORMAT" "TABLE NAME" "COUNT 1" "COUNT 2"
echo "------------------------------------"
exec 7<tables
while read A && read B
do
    read T <&7
    printf "$FORMAT" "$T" "$A" "$B"
done < infile

Result:

 TABLE NAME           COUNT 1 COUNT 2
------------------------------------
table 1                  897     897
tableName2              8642    8642
table name               888     888
SomeName               25597   25597
Anothername             7798    7798
Yet another              461     461

Thanks a lot........!!!!

Its Working...am jus making changes to my original script as this was part of it.....and hopefully can make it out...if not wil return back to bug again :stuck_out_tongue:

Please if you don mind can you explain the process....specially that exec7 one....!!!

---------- Post updated at 06:33 PM ---------- Previous update was at 06:28 PM ----------

sorry.....forgot....!!! :frowning:

Thanks to Franklin52 and albertogarcia too....!!!!
Hoping to keep getting help.. :slight_smile:

---------- Post updated at 08:05 PM ---------- Previous update was at 06:33 PM ----------

One big issue...... :eek:

data of text file as i said is like....

 
490
7127
399
0
1691
1691
3513
152
1691
3513
897
8642
888
50
25597
7798
461
29
30522
462
897
8642
888
50
25597
7798
461
29
30522
462

and for the above table it should be like from up to down and then next column...

490        897       897
7127      8642      8642
399        888       888
0           50         50
1691      25597    25597  
1691      7798      7798
3513      461        461
152        29         29
1691      30522     30522
3513      462        462

I am really sory for this mistake in communicating.....please help me out with this....!!!!

Thanks alot...!

Are the number of rows or the number of columns relevant ?
It's because the file doesn't seem to have any structure.

i=1
while read num
do
        echo "Tab$i     $num    \c"
        read num
        echo "$num"
        i=$(($i+1))
done < file_containg_nums

file doesnt have any special structure....it is just like some nos in each line as shown.....
no of rows is 29 for numbers and 10 for table names....
need to arrange them like 10 rows, each table having three nos beside it.
And the nos in file are to be used from top to bottom as shown previously...!!!!

Try this:

awk -F"\t" '{a[NR%10]=a[NR%10]?a[NR%10] FS $0:$0}
END{a[10]=a[0];for(i=1;i<=10;i++)print a}
' file

Thanks Sudhakaran and Franklin52

But still i am not able to use this code properly....!!! :confused:

Name of files are
1) Table_Name.txt
2) Table_data.txt

how to use them.I am really new to this sripting and trying to code....!!!

Excuse me, i'm a bit stupid but didn't understand what logic you wanted to involve in the script.

Hi Frans,

The Logic you used was very good but problem is with my data file.
Please if you can help me out with the requirement.....!

Can you try this simple way :slight_smile:

split -l 10  Table_data.txt
paste Table_Name.txt xaa xab xac

Thanks Frans.....!!!

Superb.......jus one more help...... i jus want them to be formatted so it looks neat......as table name and numbers are of different length so all is getting jumbled...!

table 897     897     490
table1        8642    8642    7127
table1111        888     888     399
table2      50      0
table3      25597   25597   1691
table four       7798    7798    1691
table five       461     461     3513
table6      29      29      152
some_more_table7  30522   30522   1691
yet ANother_last TAble      462     462     3513

Hope will not have to bug you all after this....:slight_smile:
Even if will have to, will come back and ask... :stuck_out_tongue:

I already thought so about the formatting :smiley:
I wouldn't take care of the format before being sure having the right data.
I can't answer immediately (i'm a bit overbooked) but i'm sure i'll find a nice solution. Just say me the width and alignment you want for each column.

What's this table for, a process or a human? If you want it to be machine processable, consistent formatting is much more important than pretty formatting.

Pretty human-readable format (can be adjusted)

FORM='%-20s%8s%8s%8s\n'
split -l 10  Table_data.txt
IFS=',' # use another delimiter if ';' dosn'fit your needs
printf "$FORM" "TABLE NAME" "COUNT 1" "COUNT 2" "COUNT 3"
echo
paste -d "$IFS" Table_Name.txt xaa xab xac | while read A B C D
do    printf "$FORM" "$A" $B $C $D
done

Hi.

The best automatic column alignment tool I have seen is align. It can found at align | freshmeat.net
An example of the use can be seen at the end of this script, using the frans split and paste method for this dataset:

#!/usr/bin/env bash

# @(#) s1	Demonstrate split, paste, align.
# See: http://freshmeat.net/projects/align

# Uncomment to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
# Infrastructure details, environment, commands for forum posts. 
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p specimen split paste align
set -o nounset

FILE1=${1-data1}
shift
FILE2=${1-data2}

# Sample data files with head / tail if specimen fails.
echo
specimen $FILE1 $FILE2 \
|| { echo "(head/tail)"; head -n 5 $FILE1 $FILE2; echo " ||" ;\
     tail -n 5 $FILE1 $FILE2; }

# Remove debris from previous runs.
rm xa?

echo
echo " Results:"
# split -l 10  Table_data.txt
# paste Table_Name.txt xaa xab xac
# paste data1 xaa xab xac |
split -l 10 data2
paste data1 xac xab xaa |
align -st

exit 0

Producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
specimen (local) 1.17
split (GNU coreutils) 6.10
paste (GNU coreutils) 6.10
align 1.7.0

Whole: 5:0:5 of 10 lines in file "data1"
table
table1
table1111
table2
table3
table four
table five
table6
some_more_table7
yet ANother_last TAble

Edges: 5:0:5 of 30 lines in file "data2"
490
7127
399
0
1691
   ---
7798
461
29
30522
462

 Results:
table			 897   897   490
table1			8642  8642  7127
table1111		 888   888   399
table2			  50	50     0
table3		       25597 25597  1691
table four		7798  7798  1691
table five		 461   461  3513
table6			  29	29   152
some_more_table7       30522 30522  1691
yet ANother_last TAble	 462   462  3513

As you can see it recognizes fields as alphabetic or numeric, and, with the separator option, will split fields by TABs. I adjusted the settings of paste to correspond to the output that the OP last displayed.

Adding column headings, if desired, is left as an exercise.

Best wishes ... cheers, drl

Note 1: the visual alignment of the output looks off in the second-last line compared to my terminal. The default join scheme is a combination of TAB and SPACE characters, so it looks to me as if the forum CODE block SPACE characters are somehow different in size from my terminal. I looked at the raw output and there are TABs between the alpha field and the first numeric column, but SPACES (as they should be) between the numeric columns. I'll continue to look at it, but it appears OK on my display. This is the first that I have noticed such an anomaly.

Note 2: The second last line does not include a TAB, only SPACES after the label, so I think that that is the source on the visual issue.

Note 3: Shortening the label in question produces:

table			 897   897   490
table1			8642  8642  7127
table1111		 888   888   399
table2			  50	50     0
table3		       25597 25597  1691
table four		7798  7798  1691
table five		 461   461  3513
table6			  29	29   152
ome_more_table7	       30522 30522  1691
yet ANother_last TAble	 462   462  3513

for which align then adds a TAB in that line, and columns appear aligned here. In both cases the output is visually aligned at my work display. This leads me to believe that SPACEs have a different value in CODE blocks compared to my workstation environment. I haven't used anything else other than Debian to look at this thread, so I suppose it is possible that the software chain is distorting the image for me, and that it appears OK to others. Perhaps I'll try looking at it with a Mac and a browser different from Firefox ( or Iceweasel, as it is known in Debian-land :slight_smile: )

Note 4: From Mac OS X, Safari, both displays look fine, so the problem is on my end, somewhere in the browser or X. I may look at it more, but the issue is not critical for me.

Note 5: Back on Linux, browser Galeon. Both displays are way off.

Note 6: Linux, epiphany-browser. First display off, second OK. Seems to point to X and / or fonts.

Thanks a lot Frans....!!!
Its working fine now......would be incorporating it into my script and hope all goes fine.... :slight_smile:

Thanks to others too...all of you made my work easy.....!!!

Will keep bugging you all....!!!:stuck_out_tongue: