single column to multiple columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers single column to multiple columns
# 1  
Old 12-05-2007
single column to multiple columns

Hello,
I have a single column of data that I would like to cut/print (with awk or ...) into multiple columns at every empty row (or common character).
Input:
5.99123
5.94693
7.21383

5.95202
0.907935
5.99149

6.08427
0.975774
6.077

Output:
5.99123 5.95202 6.08427
5.94693 0.907935 0.975774
7.21383 5.99149 6.077

Any help is appreciated.
Al
# 2  
Old 12-05-2007
don't really know what 'common character' is, but.....
Code:
nawk -v RS='' '$1=$1' myInputFile

# 3  
Old 12-05-2007
Might try this:

awk 'BEGIN{MAX=0}
/^$/{NR=0; next;}
NR > MAX{MAX=NR}
{A[NR]=A[NR] " " $1}
END{for (I=1;I<=MAX;I++) {print substr(A[I],2)}}'
# 4  
Old 12-05-2007
Thanks for the replies. Unfortunately, the nawk example prints each row (between empty rows) as a new column, not as a single column (of rows between empty rows). The awk example is not working for me either, it prints blank rows no numbers -- please detail, row by row, what is meant to happen.
# 5  
Old 12-05-2007
Hi.

With commands:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate split file on context=empty line, paste into columns.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash csplit paste

echo

FILE=${1-data1}

# Remove debris from previous runs.
rm -f xx*

echo
csplit -k -z $FILE '/^[         ]*$/'+1 '{*}'

echo
ls xx*

echo
echo " Output:"
paste -d" " xx*

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
csplit (coreutils) 5.2.1
paste (coreutils) 5.2.1


25
26
23

xx00  xx01  xx02

 Output:
5.99123 5.95202 6.08427
5.94693 0.907935 0.975774
7.21383 5.99149 6.077

The blank line goes at the end of each segment. See man pages for details ... cheers, drl
# 6  
Old 12-05-2007
ah, I see what you mean now....

nawk -f ag.awk myInputFile

ag.awk:
Code:
BEGIN {
  RS=""
}
{
  for(i=1; i<=NF; i++)
    a[i] = (i in a) ? a[i] OFS $i : $i
}
END {
  for(i=1; i in a; i++)
     print a[i]
}

# 7  
Old 12-05-2007
Perhaps use paste...
Code:
$ cat file1
5.99123
5.94693
7.21383

5.95202
0.907935
5.99149

6.08427
0.975774
6.077

$ paste -s -d '   \n' file1
5.99123 5.94693 7.21383
5.95202 0.907935 5.99149
6.08427 0.975774 6.077
$

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to generate one long column by merging two separate two columns in a single file?

Dear all, I have a simple question. I have a file like below (separated by tab): col1 col2 col3 col4 col5 col6 col7 21 66745 rs1234 21 rs5678 23334 0.89 21 66745 rs2334 21 rs9978 23334 0.89 21 66745 ... (4 Replies)
Discussion started by: forevertl
4 Replies

2. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

3. Shell Programming and Scripting

break from a single list into multiple columns

Hi Guys, I am prety new to the hell scripting world. I am running some grep/cut commands and extracting from a csv file into a list. But the final product I need is that the whole list that I now have has to be broken and separated into columns. Say what I now have extracted is a list of... (6 Replies)
Discussion started by: h_rishi
6 Replies

4. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Multiple columns to a single column

I have this input: 10 22 1 100 11 22 10 1 50 14 3 1 100 23 3 1 100 24 15 1 100 10 22 5 3 1 33.333 11 22 1 100 It has an inconsistent number of fields but the last field is determined by 100/(NF-2) using awk. I want to take this multiple columned input file and transform so that... (2 Replies)
Discussion started by: mdlloyd7
2 Replies

6. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

7. Shell Programming and Scripting

How to convert a single column into several columns?

Hi I have a ksh script which gives me the output as a single column with several rows like: AAA BBB CCC DDD EEE FFF GGG HHH III I want to be able to create a new file from this file which allows me to set the number of rows and columns in the new file, i.e. for this example, if I... (30 Replies)
Discussion started by: pinpe
30 Replies

8. Shell Programming and Scripting

how to introduce a space in a single column without distrubing the other columns

Hello Experts, I am new to this forum, I would like to do the following changes in one of the column of a txt file, which is having around 9 column. For example, column 3 is having letters like this AB11 AB12 C CA CB AC1 AC2 I would like to convert the same column as follows ... (5 Replies)
Discussion started by: Fredrick
5 Replies

9. UNIX for Dummies Questions & Answers

How to convert a single column into several rows and columns?

I have a program which gives me the output as a single column with hundreds of rows like: 213 314 324 324 123 I want to be able to create a new file from this file which allows me to set the number of rows and columns in the new file, i.e. for this example, if I specify 3 rows and 2... (5 Replies)
Discussion started by: ashton_smith
5 Replies

10. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies
Login or Register to Ask a Question