Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Rearranging columns in multiple text files

by MVRS (Acolyte)
on Mar 18, 2013 at 10:12 UTC ( #1024002=perlquestion: print w/ replies, xml ) Need Help??
MVRS has asked for the wisdom of the Perl Monks concerning the following question:

HI MONKS I WANT TO EXTRACT AND REARRANGE COLUMNS IN MULTIPLE TAB SEPERATED FILES , HERE I WROTE A CODE THAT USES LINUX COMMAND THRU PERL , PLEASE HELP IN FIXING THESE ERRORS

#!/usr/bin/perl use strict; use warnings; my $file; my @files; my $parse; my @files = <*>; foreach $file(@files) # chomp ($file); { $parse = system qq(paste <(cut -f1,2,13 $file) <(cut -f12 $file)); }

Comment on Rearranging columns in multiple text files
Download Code
Re: Rearranging columns in multiple text files
by marto (Chancellor) on Mar 18, 2013 at 10:25 UTC
      am sorry i want to apply the same command that rearranges columns in multiple files , i dont understand whats wrong in applying perl to run a linux command on multiple files , and previously i asked question to run linux commands sequentially , because am using a tool that runs on linux commands , so am using perl that handles multiple files , and am not regular programmer, ofcourse am spending time on tutorials , but am confusing in applying them properly ......

        "am sorry i want to apply the same command that rearranges columns in multiple files , i dont understand whats wrong in applying perl to run a linux command on multiple files"

        There's nothing wrong with using perl to do this, as you're simply using it as a wrapper around some shell commands I fail to see how this is useful to you without learning the basics of the tool (perl) you've chosen to use. I appreciate that this may be confusing to begin with, I strongly suggest you invest time in learning the basics. If you have a specific problem it's always best to point it out rather than say "PLEASE HELP IN FIXING THESE ERRORS" when you don't actually list any errors.

        Had you run this code you've provided you see a warning message with a line number, and a syntax error again with a line number. What problems do you have understanding these messages? Did you read the advice given the last time you asked about running system commands? The links previously given (in multiple replies) list FAQs which provide examples of how to achieve this using perl, for various scenarios. Super Search will help find related problems.

Re: Rearranging columns in multiple text files
by igelkott (Curate) on Mar 18, 2013 at 11:27 UTC

    Nothing wrong with cut or calling unix commands in general but may as well use the normal Perl commands once you've started. In particular, look into split so for each file you could do something like:

    @a = split(/\t/); print "$a[0]\t$a[1]\t$a[12]\t$a[11]\n";
    There are fancier ways to do this but seems like it'd be best to keep it simple to begin with. The other parts depend on whether you are writing all results to a single file or operating on each file individually, etc.

Re: Rearranging columns in multiple text files
by j0se (Pilgrim) on Mar 18, 2013 at 13:32 UTC

    Hi, let's suppose we have tab-separated data like that in the lines after the __DATA__ token and we want to swap columns two and three:

    use strict; use warnings; while (<DATA>) { # go through data line by line chomp; # don't forget to remove newline character my @fields = split /\t+/; print join "\t", # join fields with tab @fields[ 0, 2, 1 ], # select fields using array slice "\n"; } __DATA__ Col1 Col2 Col3 a b c 1 2 3

    To work with files, just

    • add $^I = ".bak"; before the while loop to edit files in place keeping backups
    • replace <DATA> with <> (diamond operator)

    and then run the program like this:

    $ perl script.pl *.dat

    Excellence is an art won by training and habituation: we do not act rightly because we have virtue or excellence, but we rather have these because we have acted rightly. -- Will Durant

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024002]
Approved by mr_mischief
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-07-31 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (245 votes), past polls