Modify blocks of text by printing missing columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify blocks of text by printing missing columns
# 1  
Old 11-19-2014
Linux Modify blocks of text by printing missing columns

Hi Experts,

I have a problem where I want to print missing columns (3,4) within a block of text. Each block is separated by "###". Some rows have missing column 3 and 4 which should be same as the previous value in column 3 and 4. The file is space delimited.
For example:

INPUT
Code:
###
625204690_cc   100.00%   568928245_mm   100.00%
625204692_cc   100.00%   568928247_mm   100.00%
625204688_cc   98.76%
625205698_cc   95.76%
625204600_cc   100.00%   568928555_mm   100.00%
###
625287481_cc   100.00%   568925788_mm   100.00%
625181641_cc   75.61%   568925790_mm   99.50%
124487133_cc   70.91%

OUTPUT
Code:
###
625204690_cc   100.00%   568928245_mm   100.00%
625204692_cc   100.00%   568928247_mm   100.00%
625204688_cc   98.76%   568928247_mm   100.00%                            
625205698_cc   95.76%   568928247_mm   100.00%
625204600_cc   100.00%   568928555_mm   100.00%
###
625287481_cc   100.00%   568925788_mm   100.00%
625181641_cc   75.61%   568925790_mm   99.50%
124487133_cc   70.91%   568925790_mm   99.50%

I'll be really thankful for your help.

Last edited by mira; 11-19-2014 at 08:20 AM.. Reason: formating
# 2  
Old 11-19-2014
Code:
awk '/#/ { for(N=2; N<=4; N++) delete A[N] ; print ; next }
        { for(N=2; N<=4; N++)
           {
                if($N) { A[N]=$N }
                else if(N in A) $N=A[N]
           }
} 1' infile > outfile

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-19-2014
Thanks a lot Corona! It works perfect Smilie. It would be great if you can briefly explain the code.

Thanks again for your help.
# 4  
Old 11-19-2014
Far from being an awk expert, but I managed to patch following together:
Code:
awk '$0 ~ /^#/ {print}
    NF==4 {f3=$3;f4=$4;print $0}
    NF==2 {print $0"   "f3"   "f4}' input

Explanation:
$0 ~ /^#/ {print}
If line starts with hash "#", print it unmodified

NF==4 {f3=$3;f4=$4;print $0}
If line has four fields, assign value of field 3 to variable f3, assign value of field 4 to variable f4, print the whole line unmodified

NF==2 {print $0" "f3" "f4}
If line has two fields, print the whole line unmodified, append values stored in f3 and f4

Hope this helps.
This User Gave Thanks to junior-helper For This Post:
# 5  
Old 11-19-2014
Quote:
Originally Posted by Corona688
Code:
awk '
# If a line is ###, forget old values and print it, then get the next line
/#/ { for(N=2; N<=4; N++) delete A[N] ; print ; next }
# Loop over fields 2 through 4
        { for(N=2; N<=4; N++)
           {
                # If the field exists, remember it in array A
                if($N) { A[N]=$N }
                # If it does not, pull it from array A
                else if(N in A) $N=A[N]
           }
# Print all lines, the 1 is an implied if(1) { print }
} 1' infile > outfile

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 11-20-2014
Thanks a lot! Corona and Junior-helper for explanation. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Swapping columns in specific blocks

Hi all, I hope all you guys have a great new year! I am trying to swap 2 columns in a specific block of a file. The file format is: Startpoint: in11 (input port) Endpoint: out421 (output port) Path Group: (none) Path Type: max Point ... (5 Replies)
Discussion started by: jypark22
5 Replies

2. Shell Programming and Scripting

Printing every alternate columns in a text file

Hi, I have a big matrix with 1 million columns and 21 rows, which is a tab delimited file. I would like to print every alternate columns into a new file. For each row, the start column will be always the first column. For example the input file 1 2 4 5 8 0 7 9 2 5 6 3 0 6 9 2 3 6 3 6 6 0... (3 Replies)
Discussion started by: Kanja
3 Replies

3. UNIX for Dummies Questions & Answers

Delete data blocks based on missing combinations

Hello masters, I am filtering data based on completeness. A (Name , Group) combination in File2 is only complete when it has data for all subgroups specified in File1. All incomplete (Name , Group) combinations do not appear in the output. So for example , Name1 Group 1 in File2 is... (6 Replies)
Discussion started by: senhia83
6 Replies

4. Shell Programming and Scripting

Adding and removing blocks of text from file

Hello all, short story: I'm writing a script to add and remove dns records in dns files. Its on a RHEL 5.5 So far i've locked up the basic operations in a couple of functions: - validate the parameters - search for existant ip in file when adding - search for existant name records in... (6 Replies)
Discussion started by: maverick72
6 Replies

5. Shell Programming and Scripting

How can I modify my script to include or substitute missing data?

Let me start off by saying I am a self taught sometimes scripter so what you will see below won't be pretty. I have created a script to parse through a file with a large amount of data and simply pull out what I need. In doing this I create several files and then paste them together in order to... (2 Replies)
Discussion started by: fsanchez
2 Replies

6. Shell Programming and Scripting

Working with individual blocks of text using awk

Hi, I am working with CVS log data and have some data as follows. RCS file: /cvsroot/eclipse/org.eclipse.debug.core/core/org/eclipse/debug/core/IBreakpointListener.java,v head: 1.14 branch: locks: strict access list: keyword substitution: o total revisions: 15; selected... (3 Replies)
Discussion started by: sandeepk1611
3 Replies

7. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

8. Shell Programming and Scripting

How to read text in blocks

Hi, I have file which contains information written in blocks (every block is different). Is it possible to read every block one by one to another file (one block per file). The input is something like this <block1> <empty line> <block2> <empty line> ... ... ... <block25> <empty... (0 Replies)
Discussion started by: art84_)LV
0 Replies

9. Shell Programming and Scripting

extract blocks of text from a file

Hi, This is part of a large text file I need to separate out. I'd like some help to build a shell script that will extract the text between sets of dashed lines, write that to a new file using the whole or part of the first text string as the new file name, then move on to the next one and... (7 Replies)
Discussion started by: cajunfries
7 Replies

10. Shell Programming and Scripting

Delete blocks of lines from text file

Hello, Hello Firends, I have file like below. I want to remove selected blocks say abc,pqr,lst. how can i remove those blocks from file. zone abc { blah blah blah } zone xyz { blah blah blah } zone pqr { blah blah blah } (4 Replies)
Discussion started by: nrbhole
4 Replies
Login or Register to Ask a Question