Filling in the empty columns with the rows above them


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Filling in the empty columns with the rows above them
# 1  
Old 03-05-2012
Filling in the empty columns with the rows above them

I have a text file that looks like the following:

Code:
   
    5.644  39.2%  0.00    0.50  rs1695626      4  -0.003   0.21%   0.03    0.73
                                rs1763326      3  -0.001   0.03%   0.00    0.89
    5.645  39.2%  0.00    0.50   rs770721      2  -0.002   0.07%   0.01    0.84
    5.646  39.2%  0.00    0.50   rs770720      3  -0.001   0.01%   0.00    0.94

In this file, some rows do not contain any numbers or values in their first four columns like row number 3. I would like to fill in those cells with the cells above them so that the text file looks like this:
Code:
    5.644  39.2%  0.00    0.50  rs1695626      4  -0.003   0.21%   0.03    0.73
    5.644  39.2%  0.00    0.50  rs1763326      3  -0.001   0.03%   0.00    0.89
    5.645  39.2%  0.00    0.50   rs770721      2  -0.002   0.07%   0.01    0.84
    5.646  39.2%  0.00    0.50   rs770720      3  -0.001   0.01%   0.00    0.94

How can I do that? Thanks!

Last edited by joeyg; 03-05-2012 at 04:16 PM.. Reason: Please wrap scripts and data with CodeTags
# 2  
Old 03-05-2012
Try:
Code:
awk 'NF==6{sub(" {"length(p)"}",p)}NF==10{p=$0;sub($5 ".*",x,p)}1' infile

-or-
Code:
awk 'NF==6{sub("[ \t]*" $1,p$1)} NF==10{p=$0;sub($5 ".*",x,p)}1' infile

# 3  
Old 03-07-2012
These codes do not seem to work. Does anyone have any other suggestions? Thanks!
# 4  
Old 03-07-2012
In what way do they "not work"? Details please.

Are these input files tab-separated?
# 5  
Old 03-07-2012
I just get the same text file back. No they are not tab separated, they have irregular spacing. How can I change them to have tab separation? Thanks!
# 6  
Old 03-07-2012
This should fill in the 32 characters when absent, then make the output tab-separated:
Code:
awk 'BEGIN { OFS="\t" }; NF != 10 { $0=substr(LAST, 1, 32) substr($0, 33); $1=$1; print ; next }; { LAST=$0; $1=$1 } 1'

# 7  
Old 03-07-2012
For files without missing columns, where no spaces are in the columns themselves, you can do
Code:
tr -s ' ' '\t' < input > output

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

2. UNIX for Dummies Questions & Answers

Filling in the empty columns with rows above them

Hi, I have a text file where some of the values in columns are missing: Input: QhFudW3dKSYL2NTQUA 1 11133999-11134048 uhIp9KxB6USIy16CEY 1 11126760-11126774 11126775-11126805 11128065-11128068 6epeZ6CTNW4Au8uXys 1 24480823-24480872 Tp3nvutucBZ13CjuXc 1 155204870-155204891... (3 Replies)
Discussion started by: evelibertine
3 Replies

3. UNIX for Dummies Questions & Answers

Filling empty cells

How do you fill empty cells that do not have any data in them with "X" in a tab delimited text file? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

4. UNIX for Dummies Questions & Answers

Filling in empty columns with "X"

I have a text file which includes empty columns with missing values. I want to fill the empty columns with the character "X". How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

Filling a tab-separated file with known missing entries in columns

Hello all, I have a file which is tab separated like that: PHE_205_A TIP_127_W ARG_150_B MET_1150_A TIP_12_W VAL_11_B GLU_60_A TIP_130_W ARG_143_B LEU_1033_A TIP_203_W ARG_14_B SER_1092_A TIP_203_W THR_1090_A TIP_203_W SER_1092_A TIP_25_W ... (6 Replies)
Discussion started by: TheTransporter
6 Replies

6. UNIX for Dummies Questions & Answers

Filling the empty columns in a fixed column file

Hi, I have a file with fixed number of columns (total 58 columns) delimeted by pipe (|). Due to a bug in the application the export file does not come with fixed number of columns. The missing data columns are being replaced by blank in the output file. In one line I can have 25 columns (33... (1 Reply)
Discussion started by: yale_work
1 Replies

7. UNIX for Dummies Questions & Answers

Filling in the empty columns using the rows above them

I have a text file that looks like the following: rs529715 CFD rs1550758 CIDEB Magmas rs12542019 CPNE1 RBM12 rs10515181 CPNE1 RBM12 rs4411112 CPT1A Some rows have 1 column, whereas some have 2. The ones that have 2 columns have one column (1st column) that start with rs. I need to... (6 Replies)
Discussion started by: evelibertine
6 Replies

8. UNIX for Dummies Questions & Answers

Deleting all rows with empty columns

I have a text file that looks like this: 1 rs523634 8.22486 1 1 rs585160 8.22488 1 rs497228 8.2249 1 1 rs600933 8.225 1 rs480106 8.22531 1 rs600199 8.22533 1 rs529015 8.22534 1 rs598894 8.22534 I want to delete the rows with empty... (2 Replies)
Discussion started by: evelibertine
2 Replies

9. Shell Programming and Scripting

Compare columns and rows with template, and fill empty slots.

Hi, I'm working on a script that will take the contents of a file, that is in a row and column format, and compare it to a arrangment file. Such that if there is any or all blanks in my content file, the blank will be filled with a flag and will retain the row and column configuration. Ex. ... (2 Replies)
Discussion started by: hizzle
2 Replies

10. Shell Programming and Scripting

Filling in missing columns

Hi all, I have a file that contains about 1000 rows and 800 columns. Nearly every row has 800 columns but some DONT. I want to extend the rows that dont have values with NA's. Here is an example: my file bob 2 4 5 6 8 9 4 5 tar 2 4 5 4 3 2 9 1 bro 3 5 3 4 yar 2 ... (7 Replies)
Discussion started by: gisele_l
7 Replies
Login or Register to Ask a Question