anisha3 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!!!! I have many files and I need 21 and 4 column of every file to be printed to a single file...I used this command but its printing in single files...

perl -F"\t" -lane '$, = ","; print $F[0], $F[4]' AF1.gcount > Af1gcoun +t.csv

Please help me how can i make changes and get my desired output...my out put should have all the columns printed with headers of files name..

Replies are listed 'Best First'.
Re: Printing columns to a single file
by BrowserUk (Patriarch) on Mar 05, 2014 at 15:21 UTC
    I need 21 and 4 column of every file ... print $F[0], $F[4]

    Which is it: 21st and 4th columns, or first and fifth columns?

    I have many files ...I used this command but its printing in single files...

    The command you show processes a single input file and produces a single output file. Do you know about wild cards?

    my out put should have all the columns printed with headers of files name..

    What bit of the command you've shown do you expect to produce "headers of files name."?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Hi I would löike to extract column 0 and 4 of every file and print them in a single file ex: Gene Id fEMALE mALE 00121 251 252 2213 3 224 .... GENE ID IS COMMON IN EVERY FILE i dont mine using long script but when i tried my code has errors

        but when i tried my code has errors

        Show us your failing code and let us help you correct your errors.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Printing columns to a single file
by kennethk (Abbot) on Mar 05, 2014 at 15:35 UTC

    I am having difficulty understanding what you have written. It would be easier if you included sample input and output, wrapped in code tags, as described in How do I post a question effectively?.

    Is there a reason you are committed to using a one-liner instead of a more traditional multi-line script? That could give you a great deal of control in how you cycle over your files (opendir, readdir) and how they are formatted. As well, since you are dealing with CSV, it might make sense to use Text::CSV in case there is escaping. My general rule is I only use a one-liner when I'm doing a quick task once - by posting a question, it feels like you no longer meet the quick criterion.

    If you need to know what file name you are dealing with in a -n context, it's stored in $ARGV. So maybe you want something closer to

    perl -F"\t" -lane '$, = ","; print $ARGV, $F[21], $F[3]' *.gcount > Af +1gcount.csv

    Update: Fixed broken link. Thanks NaN.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I have files like file 1,file 2,file 2 etc like in hundreds...It has data in columns so I would like to extract 0 and 4 column from every file then print it in to a single .txt or .csv file... the code i used it was useful to extract the desired columns and print it in seperate single files then again i have to generate a code and complie them...i am asking that if theres any posibility to include every thing in single code as am not much familiar with this sort of thing...I hope you understand

        Note that you did not follow any of the advice I listed above. You did not show a sample of input. You did not show a sample of output. You did not explain why you are using a one-liner. You did not say why the provided code modification didn't do what you needed. Why are you making it hard for me to help you?
        I have files like file 1,file 2,file 2 etc like in hundreds...
        You can use opendir and readdir to cycle over, as I said previously, or use glob if you are so inclined. -n uses <> to read files (see I/O Operators), so it will implicitly cycle over the argument list - thus *.gcount will be expanded by the shell into a full file list and get cycled over.
        It has data in columns so I would like to extract 0 and 4 column from every file
        So why aren't you using Text::CSV to read it?
        i am asking that if theres any posibility to include every thing in single code
        A great deal is possible. That doesn't make it a good idea. After all this time and effort, a simple script would have been written and done already. That certainly could be considered a "single code". Why are you hung up on one-liners?

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.