We help IT Professionals succeed at work.

Column Reorder if condition met

mateinone
mateinone asked
on
Okay I have an input file and if the first column does not meet a variable
I want to move that column to the 3rd column

So lets say the following

CHECK07  ACC USER1    TYPE01   DATA01      2011-10-10
CHECK08  ACC USER1   TYPE01   DATA02      2011-10-10
CHECK09  ACC USER2    TYPE01   DATA03      2011-10-10
CHECK10  ACC USER2    TYPE02   DATA04      2011-10-10

if field one does not eq ACC, then I would want the file reordered to be
ACC USER1    CHECK07  TYPE01   DATA01      2011-10-10
ACC USER1   CHECK08  TYPE01   DATA02      2011-10-10
ACC USER2    CHECK09  TYPE01   DATA03      2011-10-10
ACC USER2    CHECK10  TYPE02   DATA04      2011-10-10

=======

The file needs to keep the same name and I am already doing a number of edits as part of this script, so basically I think I would just need the search/replace string I think...
here is an example of function I have there to do most of the editing, ideally it would be in that loop


sub edit_file {
open (FILE, "<$optfile") or die "Can't open $optfile: $!\n";
@lines = <FILE>;
close FILE;
open (STDOUT, ">$optfile") or die "Can't open $optfile: $!\n";
for ( @lines ) {
    s/\x0D//g;
    s/$/\tYES\t$filedate/;
    s/\tYES\t$filedate\tYES\t$filedate$/\tYES\t$filedate/;
  if($table eq "db") {
    s/0{4}\t[0-9]{4}\-[0-9]{2}\-[0-9]{2}\t[0-9]{2}\:[0-9]{2}\:[0-9]{2}\tYES\t$filedate/0000\tYES\t$filedate/g;
  }
  s/\\/\\\\/g;
  s/"\tape"/\\\tape/g;
  print;
}
close STDOUT;
open (STDOUT, ">&2") or die "Can't open STDOUT: $!\n";
print "edited file $optfile";
print "\n";
}



Comment
Watch Question

Author

Commented:
Okay looks like I have sorted this out

Author

Commented:
Just a question.. do I just delete this question?
Nem SchlechtIT Supervisor
Top Expert 2009

Commented:
I think you can just click on "Request Attention" and ask the moderator to delete it.
   if( m{^$account+} eq "" ) {
      s/((^\S+)\t+(\S+)\t(\S+))/$3\t$4\t$2/g ;
    }

That is the solution I needed, though could have gone without outside brackets

Author

Commented:
Worked out solution
Just added to allow question to be closed