Link to home
Start Free TrialLog in
Avatar of mateinone
mateinoneFlag for Australia

asked on

Column Reorder if condition met

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";
}



Avatar of mateinone
mateinone
Flag of Australia image

ASKER

Okay looks like I have sorted this out
Just a question.. do I just delete this question?
Avatar of Nem Schlecht
I think you can just click on "Request Attention" and ask the moderator to delete it.
ASKER CERTIFIED SOLUTION
Avatar of mateinone
mateinone
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked out solution
Just added to allow question to be closed