# Changing data format from column into array

**URL:** <https://community.unix.com/t/changing-data-format-from-column-into-array/318533>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [October 19, 2012, 4:09pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533 "2012-10-19T16:09:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShiGua](https://community.unix.com/letter_avatar/shigua/32/5_5575768a8748004e209b776fc1b2916d.png) [@ShiGua](https://community.unix.com/u/ShiGua)\
**Post date:** [October 19, 2012, 4:09pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/1 "2012-10-19T16:09:19Z")

</div>

I have a file containing numbers in a column like:

10.5  
16.3  
15.7  
2.3  
46.8  
3.3  
.  
.  
.

and I was wondering if there was a way to make it show up in an array form like:

10.5 2.3  
16.3 46.8  
15.7 3.3

Let's say I want to make a new column every 100 values. How can I do that and be able to copy and paste the values into individual cells in MS Excel? If it's not possible, let me know too.

Thank you!

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [October 19, 2012, 4:13pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/2 "2012-10-19T16:13:07Z")

</div>

The 'paste' command slides files together, so you just need scripts to select the right and left columns. You too 3 and 3, but then you have ..., so is it 3 or half or ? You can pipe together the two file generators to pipe so there is no delay, temp file space limit or extra i/o overhead.

---

<div class="post-metadata">

**Author:** ![ShiGua](https://community.unix.com/letter_avatar/shigua/32/5_5575768a8748004e209b776fc1b2916d.png) [@ShiGua](https://community.unix.com/u/ShiGua)\
**Post date:** [October 19, 2012, 4:19pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/3 "2012-10-19T16:19:00Z")

</div>

Oh sorry, those are decimal values. Let me give a better example of what I want to do.

I have a file that contains:

1  
2  
3  
4  
5  
6  
7  
8  
9

and I want to make a file that contains:

1 2 3  
4 5 6  
7 8 9

and for me to be able to copy that 3 by 3 array and paste it into a 3 by 3 table in excel.

Are you saying the 'paste' command will make a new file with data from each file displayed side by side? If so, is there a way I can take a file and make new files containing every 100 lines of the original file?

Thanks again.

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [October 19, 2012, 4:32pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/4 "2012-10-19T16:32:27Z")

</div>

```nohighlight
awk '{printf $1 (!(NR % cols) ? c="\n" : c=" "); } END {print "";}' cols=3 infile

```

---

<div class="post-metadata">

**Author:** ![ShiGua](https://community.unix.com/letter_avatar/shigua/32/5_5575768a8748004e209b776fc1b2916d.png) [@ShiGua](https://community.unix.com/u/ShiGua)\
**Post date:** [October 19, 2012, 4:36pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/5 "2012-10-19T16:36:14Z")

</div>

Sorry, just checking. cols will be the number of entries in each column right? Not the number of columns? And what's the $1 for?

---

<div class="post-metadata">

**Author:** ![scottaazz](https://community.unix.com/letter_avatar/scottaazz/32/5_5575768a8748004e209b776fc1b2916d.png) [@scottaazz](https://community.unix.com/u/scottaazz)\
**Post date:** [October 19, 2012, 4:40pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/6 "2012-10-19T16:40:25Z")

</div>

Or you can use the paste command directly (each dash represents a column). You can use the -d option of paste to set the delimiter (default is tab)

```nohighlight
cat yourfile | paste - - -

```

if yourfile contained

```nohighlight
1
2
3
4
5
6
7
8
9
 
and you did
cat yourfile | paste - - -

```

```nohighlight
You would get
1 2 3
4 5 6
7 8 9

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [October 19, 2012, 4:41pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/7 "2012-10-19T16:41:05Z")

</div>

cols is the number of columns out. $1 is the field one on the input file, as in the example provided.

---

<div class="post-metadata">

**Author:** ![Adhi](https://community.unix.com/letter_avatar/adhi/32/5_5575768a8748004e209b776fc1b2916d.png) [@Adhi](https://community.unix.com/u/Adhi)\
**Post date:** [October 19, 2012, 4:42pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/8 "2012-10-19T16:42:25Z")

</div>

> [@shigua](#):
>
> Sorry, just checking. cols will be the number of entries in each column right? Not the number of columns? And what's the $1 for?

$1 is the first field of the file..

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [October 19, 2012, 4:48pm UTC](https://community.unix.com/t/changing-data-format-from-column-into-array/318533/9 "2012-10-19T16:48:05Z")

</div>

I suppose a simple shell script could count lines read and print thos modulo 100 = 1, 2, or 0.

```nohighlight
ct=0
while read l
do
 if (( ct % 100 == 1 ))
 then
  echo "$l"
 fi
done <in_file

```

If you don't like/have pipes \<(...), you can collect each 300 lines in an array and print them out in your order.

You could prefix each line with the order you need them in (1, 4, 7, . . . 298, 2, 5, 8, . . . 299, 3, 6, 9, . . . 300, 301, 304, . . . .), sort -n them all and process the output three lines into one:

```nohighlight
while read n l1
do
 read n l2
 read n l3
 echo "$l1 $l2 $l3"
done

```

It'd make a handy C app, just give it a width and length, and it can fputs the M lines to N tmpfile() FILE\*'s, rewind, print, rewind, repeat.
