Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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:


#!/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)); }

Replies are listed 'Best First'.
Re: Rearranging columns in multiple text files
by marto (Bishop) 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 (Priest) 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 reisinge (Friar) 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 *.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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024002]
Approved by mr_mischief
[Corion]: hippo: Yeah, I might take that one so I don't get out of London too late if it comes to that ;)
[choroba]: she called carabinieri, the train had to stop and we had to testify to them. But we didn't miss the plane in the end.
[karlgoethebier]: good morning *
[Discipulus]: miracle choroba! and unbelivalbe carabinieri understood you
[choroba]: They even sent me a letter whether I'd be willing to come to Milano to testify in the court
[choroba]: I replied with "yes" but the trial never happened
[Discipulus]: dont be so sure choroba.. 11 years is not too long for an Eatalian judgement
[choroba]: I've moved to Prague in the meantime, they can't reach me anymore :-)
[Discipulus]: Eataly: average 8years and 7months for all 3 degrees of judgement..

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2017-11-20 09:40 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (286 votes). Check out past polls.