One Column To Two Columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting One Column To Two Columns
# 1  
Old 12-30-2012
One Column To Two Columns

Having difficulty reformatting a 1 column file into a 2 column file as shown below:

Example Input

Code:
A
01
02
05
07
08
11
B
1193941
7231222
5192121
3221312
2211149
1783289

Required Output

Code:
A  B
01 1193941
02 7231222
05 5192121
07 3221312
08 2211149
11 1783289

Field widths are always 2 in Section A and 7 in Section B.
Within each Section the field range is min 5 max 22.
Section A is in ascending numerical order
The Section titles are the only non 0-9 characters.

Any help would be much appreciated.
# 2  
Old 12-30-2012
Code:
cat filename | tr '\n' '\t' | awk ' { for (i=1; i<=NF/2; i++) { print $i, $(i+(NF/2)), "\n" } }'


Solution edited and fixed

Last edited by fuad_; 12-30-2012 at 03:52 PM.. Reason: Code tags
# 3  
Old 12-30-2012
try also:
Code:
awk '
/[A-Za-z]+/ {a[c++]=$0; n=0}
!/[A-Za-z]+/ {o[a[c-1], n++]=$0}
END {
  for (i=0; i<c; i++) printf (i<c-1) ? a[i]"\t":a[i]"\n";
  for (j=0; j<n; j++) {
    for (i=0; i<c; i++) printf (i<c-1) ? o[a[i], j]"\t":o[a[i], j]"\n";
  }
}' input.txt

# 4  
Old 12-30-2012
If there's the same number of rows each time:
Code:
$ cat file
A
01
02
05
07
08
11
B
1193941
7231222
5192121
3221312
2211149
1783289

$ split -l7 < file

$ ll
total 24
drwxr-xr-x   5 scott  staff   170 30 Dec 19:05 .
drwxr-xr-x  79 scott  staff  2686 30 Dec 19:05 ..
-rw-r--r--   1 scott  staff    70 30 Dec 19:05 file
-rw-r--r--   1 scott  staff    20 30 Dec 19:05 xaa
-rw-r--r--   1 scott  staff    50 30 Dec 19:05 xab

$ paste x*
A	B
01	1193941
02	7231222
05	5192121
07	3221312
08	2211149
11	1783289

# 5  
Old 12-30-2012
I checked it and I find a small bug and here how I fix my first solution

Code:
cat filename | tr '\n' '\t' | awk ' { for (i=1; i<=NF/2; i++) { print $i, $(i+(NF/2)), "\n" } }'

Code:
$ cat test
A
01
02
05
07
08
11
B
1193941
7231222
5192121
3221312
2211149
1783289

$ cat test | tr '\n' '\t' | awk ' { for (i=1; i<=NF/2; i++) { print $i, $(i+(NF/2)), "\n" } }'
A B 

01 1193941 

02 7231222 

05 5192121 

07 3221312 

08 2211149 

11 1783289


Last edited by Scott; 12-30-2012 at 04:34 PM.. Reason: Code tags
# 6  
Old 12-30-2012
If you want to verify each of the conditions stated and print warnings when the conditions aren't met, try the following:
Code:
#!/bin/ksh
awk '/[^[:digit:]]/ {
        h[++hc] = $0
        next
}
{       o[hc,++oc[hc]] = $0
}
END {   if(hc != 2) {
                printf("Expected 2 heading lines; found %d: ", hc)
                for(i = 1; i <= hc; i++)
                        printf("\"%s\"%s", h[i], i == hc ? "\n" : " ")
                ec = 1
        }
        if(oc[1] != oc[2] || oc[1] < 5 || oc[1] > 22) {
                printf("%s\n%s %d %s \"%s\" and %d %s \"%s\".\n",
                        "Expected 5 <= x <= 22 entries under both headings;",
                        "found", oc[1], "under heading", h[1], oc[2],
                        "under heading", h[2])
                ec += 2
        }
        printf("%-3s%s\n", h[1], h[2])
        c = oc[1] > oc[2] ? oc[1] : oc[2]
        last = -1
        for(i = 1; i <= c; i++) {
                if(i <= oc[1] && o[1, i] <= last) {
                        ec += ec % 8 < 4 ? 4 : 0
                        m = sprintf(" %d not > %d.",
                                o[1, i], last)
                } else  m = ""
                if(i <= oc[1] && length(o[1, i]) != 2) {
                        ec += ec % 16 < 8 ? 8 : 0
                        m = sprintf("%s 1st field not 2 digits.", m)
                }
                if(i <= oc[2] && length(o[2, i]) != 7) {
                        ec += ec < 16 ? 16 : 0
                        m = sprintf("%s 2nd field not 7 digits.\n", m)
                } else  m = sprintf("%s\n", m)
                printf("%s %s%s", o[1, i], o[2, i], m)
                last = o[1, i]
        }
        exit ec
}' Input

Replace /bin/ksh in #!/bin/ksh with an absolute pathname of the Korn shell (or any shell that conforms to POSIX shell command language requirements) on your system.

If you're on a Solaris system, use /usr/xpg4/bin/awk or nawk instead of awk.

The exit code will be 0 if all conditions are met, or 1 through 31 depending on which set of conditions fail.
# 7  
Old 12-30-2012
Many thanks for the very quick replies.

Scott: the number of rows varies between 5(min) and 22(max)

It's a hard choice between between fuad's second post code and rtrdx1's code.

fuad's output rows are separated by blank lines.

rdtx1's output columns are separated by a tab(?) instead of single space.

On balance I'm inclined to go with fuad's single-liner largely because I know how to delete
blank lines :-)

---------- Post updated at 05:06 PM ---------- Previous update was at 04:44 PM ----------

Don Cragun

Thanks for a very comprehensive reply but as I'm able to verify the file meets the stated requirements before reformatting I don't have to cater for all the exception conditions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

3. Shell Programming and Scripting

Several columns to two column

Dear All, I have file : input.txt HANDVEL 2201181 1000 180 19 1540 173 1581 316 1652 509 1707 653 1767 816 1834 951 1882 1100 1984 1225 2050 1331 2109 1732 ... (4 Replies)
Discussion started by: attila
4 Replies

4. Shell Programming and Scripting

Splitting the data in a column into several columns

Hi, I have the following input file 32895901-d17f-414c-ac93-3e7e0f5ec240 AND @GDF_INPUT 73b129e1-1fa9-4c0d-b95b-4682e5389612 AUS @GDF_INPUT 40f82e88-d1ff-4ce2-9b8e-d827ddb39447 BEL @GDF_INPUT 36e9c3f1-042a-43a4-a80e-4a3bc2513d01 BGR @GDF_INPUT I want to split column 3 into two columns:... (1 Reply)
Discussion started by: ramky79
1 Replies

5. Shell Programming and Scripting

one column in different columns

I have a File with these format: A1 A2 A3 A4 B1 B2 B3 B4 . . . And I wont these format: A1 A2 A3 A4 B1 B2 B3 B4 .. .. .. .. .. ... How can I do that??? thanks (1 Reply)
Discussion started by: manudbc
1 Replies

6. Shell Programming and Scripting

Separating data from one column into two columns

Hello, I have a file that contains 64,235 columns and over 1000 rows and looks similar to this: ID dad mom 1 2 3 4 5.... 64232 1234 5678 6789 AA BB CC DD EE....ZZ 1342 5786 6897 BB CC DD EE FF....AA 1423 5867 6978 CC DD EE FF GG....BB I need to leave the first three columns in... (4 Replies)
Discussion started by: doobedoo
4 Replies

7. Shell Programming and Scripting

split one column into multiple columns

hey, i have the following data: 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 (7 Replies)
Discussion started by: zaneded
7 Replies

8. UNIX for Dummies Questions & Answers

split one column into multiple columns

hey guys... Im looking to do the following: 1 2 3 4 5 6 7 8 9 Change to: 1 4 7 2 5 8 3 6 9 Did use | perl -lpe'$\=$.%3?$":"\n"' , but it doesnt give me the matrix i want. (3 Replies)
Discussion started by: zaneded
3 Replies

9. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies

10. UNIX for Dummies Questions & Answers

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... (7 Replies)
Discussion started by: agibbs
7 Replies
Login or Register to Ask a Question