Conditional reverse of certain file line order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional reverse of certain file line order
# 1  
Old 02-14-2009
Conditional reverse of certain file line order

Hi I am brand new to programming, I dont know how to go about this task, or what language is best for this...If there is an easy solution in different languages, I would love to see them. I want to learn about the steps to take on this, so Please put in comments where code is used.
I believe in the old addage, "teach a man to fish and he is fed for life"
Thank you all in advance.

I have a file that need to have some of the lines' ordering switched.
When the (x)coordinate (x,y)is even integer, need to reverse line order.

starting file
'some value' 1,1
'some value' 1,4
'some value' 1,6
'some value' 2,4
'some value' 2,5
'some value' 3,1
'some value' 4,1
'some value' 4,4
'some value' 4,8
'some value' 5,4

New File
'some value' 1,1
'some value' 1,4
'some value' 1,6
'some value' 2,5
'some value' 2,4
'some value' 3,1
'some value' 4,8
'some value' 4,4
'some value' 4,1
'some value' 5,4

Smilie Thank you all in advance for any help
# 2  
Old 02-15-2009
An approach with awk:

Code:
awk '
{split($NF,c,",")}      # split the last field ($NF) into array c (c[1]=x, c[2]=y)
!(c[1]%2) {             # use the modulo operator % to search for lines with an even x
  a[++i]=$0             # if x is even assign the line into array a
  next                  # read the next line; these lines never reach the next commands!
}
i{for(j=i;j>0;j--){     # for the next first line with an odd x (i is not 0)
    print a[j]          # .. print array "a" reversed, before the current line
  }
  i=0                   # .. and reset i
}
{print}                 # print the line with an odd x
END{                    # after the last line, check the value of i
  if(i){                # .. if i > 0 the last line is a line with an even x
    for(j=i;j>0;j--){   # .. array a is not printed yet
      print a[j]        # .. print array a
    }
  }
}
' starting_file > new_file

Regards
# 3  
Old 02-15-2009
Bug Thank you

Thank you so much Franklin, Smilie (awk is the language)
Now I have a great learning tool
I was really struggling with perl to do this, as I am a rookie. I seemed to be fairly close at times, but always came up short. Very frustrating, I really appreciate, your help...

So as to not be a complete leach, I should probably try to convert the code to perl or sed. Im new to all this tho..

Smilie Does the ($NF) split last field in perl and/or sed too, or is every language completely different?

thank you
# 4  
Old 02-15-2009
Hi again,
1 last thing
I know in perl the 1st field of an array is [0], is this different in awk?
Or did I miss something?
You have [1] as the 1st array. Im just trying to learn more.

thx
# 5  
Old 02-15-2009
May as well post it here too:

Code:
use strict;
use warnings;
my @sort_keys;
open (my $IN, 'd.dat') or die "$!"; 
my @lines = <$IN>;
close $IN;
open (my $OUT, ">", 'out.dat') or die "$!";
foreach my $line (@lines) {
   my ($x,$y) = $line =~ / (\d+),(\d+)\s*$/;
   if ($x % 2) {
      if (@sort_keys) {
         print $OUT map {$_->[1]} sort {$b->[0] <=> $a->[0]} @sort_keys;
         @sort_keys = ();
         print $OUT $line;
      }
      else {
         print $OUT $line;
      }
   }
   else {
      push @sort_keys, [$y, $line];
   }
}
close $OUT;

# 6  
Old 02-15-2009
Quote:
Originally Posted by perlrookie
Hi again,
1 last thing
I know in perl the 1st field of an array is [0], is this different in awk?
Or did I miss something?
You have [1] as the 1st array. Im just trying to learn more.

thx
The split function of awk splits a field into an array and the first index of the array = 1.
You can find some awk and perl tutorials at this link:

https://www.unix.com/answers-frequent...tutorials.html

Regards
# 7  
Old 02-15-2009
thank you

did you see that I have many syntax errors testing out this code...
Is it me or the code?
My guess is I am doing something wrong....
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Reverse the order of a list of file names (but not sort them alphabetically or numerically)

Hello all, I have a list of file names in a text document where each file name consists of 4 letters and 3 numbers (for example MACR119). There are 48 file names in the document (they are not in alphabetical or numerical order). I would like to reorder the list of names so that the 48th name is... (3 Replies)
Discussion started by: MDeBiasse
3 Replies

3. Shell Programming and Scripting

print in reverse order

Hi, I want to print the item in reverse order such that the output would look like 00 50 50 23 40 22 02 96 Below is the input: 00 05 05 32 04 22 20 69 Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: reignangel2003
5 Replies

4. Shell Programming and Scripting

How to get fields in reverse order?

i am having lines like below seperated by "|" (pipe) abc|xyz 123|567 i have to get the above in reverse order xyz|abc 567|123 Pls help (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. UNIX for Dummies Questions & Answers

How can I list the file under a directory both in alphabetical and in reverse alphabetical order?

How can I list the file under current directory both in alphabetical and in reverse alphabetical order? (1 Reply)
Discussion started by: g.ashok
1 Replies

6. Shell Programming and Scripting

How to manipulate first column and reverse the line order in third and fourth column?

How to manipulate first column and reverse the line order in third and fourth column as follws? For example i have a original file like this: file1 0.00000000E+000 -1.17555359E-001 0.00000000E+000 2.00000000E-002 -1.17555359E-001 0.00000000E+000 ... (1 Reply)
Discussion started by: Max Well
1 Replies

7. Shell Programming and Scripting

creating a file in reverse order of another file

Hi, I have a requirement where i need to write a script to create the new file from the given input file with the data in reverse order (bottom to top) Sample data: Input File-------------- Java VB Oracle Teradata Informatica Output file:----------------- Informatica Teradata Oracle... (3 Replies)
Discussion started by: srilaxmi
3 Replies

8. Shell Programming and Scripting

read line in reverse order from file

dear all i want to read 5th no of line from last line of file. kindly suggest me possible ways. rgds jaydeep (2 Replies)
Discussion started by: jaydeep_sadaria
2 Replies

9. Shell Programming and Scripting

sort a file in reverse order

I a file with log entries... I want to sort it so that the last line in the file is first and the first line is last.. eg. Sample file 1 h a f 8 6 After sort should look like 6 8 f a h 1 (11 Replies)
Discussion started by: frustrated1
11 Replies
Login or Register to Ask a Question