Sponsored Content
Top Forums Shell Programming and Scripting Make Separated files from a single matrix - Perl Post 302818227 by balajesuri on Friday 7th of June 2013 07:04:17 AM
Old 06-07-2013
This will work for any number of columns you have after col #4.

Code:
#! /usr/bin/perl -w
use strict;

# Open the input.txt for reading in filehandle FILE
open FILE, "< input.txt";
my $totCols = 0;
my @hdrFields;
my @fields;
my @refs;

 # Read each line of input.txt from the filehandle
while (<FILE>) {
    # Remove the new line character from the line using chomp
    chomp;
    # If it's the first line being read then...
    if ($. == 1) {
        # Split the first line according to whitespaces and store the fields in array @hdrFields
        @hdrFields = split /\s+/;
        # Store the total number of columns in $totCols.
        # $#hdrFields refers to the last index number of attay @hdrFields
        $totCols = $#hdrFields + 1;
        
        # First 4 columns are fixed. So with 5th column as 1, iterate till the last column.
        # In the given example, there are 2 columns after col #4.
        # The below for-loop opens that many files for writing the output. In given example its 2.
        for (1 .. ($totCols - 4)) {
            # Reference to filehandles are stored in array @refs
            open ($refs[$_], "> $hdrFields[4 - 1 + $_].wig");
        }
        
        # After parsing the first line and opening the file handles for writing skip to the next line of file.
        # This will prevent the below code from being executed.
        next;
    }
    
    # For all lines after the header line, do the below:
    # First split the line according to whitespaces and store the fields in array @fields.
    @fields = split /\s+/;
    
    # Again, iterate through that many numbers equivalent to the number of columns after 4 (since first 4 columns are fixed)
    for (1 .. ($totCols - 4)) {
        # Retrieve the reference to file from array @refs
        my $fh = $refs[$_];
        # Print to corresponding file, first 4 fixed columns then the corresponding column. In this case, either col #5 or col #6.
        print $fh join("\t", @fields[1..3]) . "\t$fields[4 - 1 + $_]\n";
    }
}

# Finally close all the files that were opened for writing
for (1 .. ($totCols - 4)) {
    close $refs[$_];
}

# Finally close the input file that was opened for reading
close FILE;

After running this script, find the files in current directory:
Code:
[user@host ~]$ ls -l HEL25E.wig TRIP1.wig
-rw-r--r-- 1 user domain Users 226 Jun  7 16:32 HEL25E.wig
-rw-r--r-- 1 user domain Users 221 Jun  7 16:32 TRIP1.wig
[user@host ~]$


Last edited by balajesuri; 06-07-2013 at 08:11 AM..
This User Gave Thanks to balajesuri For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate lines in a single '|' separated line

Hi I have a file with contents like china india france japan italy germany . . . . etc.... I want the output as china|india|france|japan|italy|germany|.|.|. (3 Replies)
Discussion started by: hidnana
3 Replies

2. Shell Programming and Scripting

BASH: print matrix from single array

I am creating a report in groff and need to format data from a file into a table cell. Sample data: dador,173323,bpt,jsp,39030013338878,1 dador,173323,brew,jsp,39030013338860,1 dador,173323,brew,jsp,39030013339447,1 dador,173323,brew,jsp,39030013339538,1 I would like to build a table... (12 Replies)
Discussion started by: Bubnoff
12 Replies

3. UNIX for Dummies Questions & Answers

How to make a distance matrix

Hi, I'm trying to generate a distance matrix between sample pairs for use in a tree-drawing program (example below). The example below demonstrates what I'd like to get out of the data - essentially, to calculate the proportion of positions where two samples differ. Any help much appreciated!... (1 Reply)
Discussion started by: auburn
1 Replies

4. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

5. Homework & Coursework Questions

Find the files and make them comma separated files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi All, I am new to unix, my requirement is like need to find the files like DATA_FUNCTION* and put those... (1 Reply)
Discussion started by: madsongtel
1 Replies

6. UNIX for Dummies Questions & Answers

tab-separated file to matrix conversion

hello all, i have an input file like that A A X0 A B X1 A C X2 ... A Z Xx B A X1 B B X3 .... Z A Xx Z B X4 and i want to have an output like that A B C D A X0 X1 X2 Xy B X1 X3 X4 (4 Replies)
Discussion started by: TheTransporter
4 Replies

7. Shell Programming and Scripting

Shell Code required -Output in Multiple Rows to be in single row separated by Commas -

Hola Greetings Experts , I have records spreaded across multiple lines. in attached log.txt i want output to be in 1 line like this below Atached as Output.txt. In brief Output related to 1 line is spreaded across multiple row I wanted it to be in 1 row . Please opem the file in notepad... (4 Replies)
Discussion started by: manishK
4 Replies

8. Shell Programming and Scripting

Make multiple lines into single quoted comma separated Linux

Hi, I want to change a file file1.txt: 1234 3456 2345 6789 3456 2333 4444 As, file2.txt in Linux: '1234','3456','2345','6789','3456','2333','4444' Could someone please help me. (Single liner sed, awk will be welcome!) (7 Replies)
Discussion started by: wiweq05
7 Replies

9. Shell Programming and Scripting

Insert single quote on every word separated by comma

Hello, I have a text file as:-ABC BCD CDF DEF EFGI need to convert as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG' using a unix command anybody can help me out on this. Regards, Jas Please wrap all code, files, input & output/errors in CODE tags. It makes them easier to read and preserves... (12 Replies)
Discussion started by: jassi10781
12 Replies

10. Shell Programming and Scripting

Tabbed multiple csv files into one single excel file with using shell script not perl

Hi Experts, I am querying backup status results for multiple databases and getting each and every database result in one csv file. so i need to combine all csv files in one excel file with separate tabs. I am not familiar with perl script so i am using shell script. Could anyone please... (4 Replies)
Discussion started by: ramakrk2
4 Replies
All times are GMT -4. The time now is 03:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy