Perl regex and sort trouble


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl regex and sort trouble
# 1  
Old 08-05-2011
Perl regex and sort trouble

Hi so I have these files where the first thing in them says something along the lines of "This document was accessed 'date' blah blah", I was thinking of a way to extract that date and then sort the files based on that date.

My question is how do I get rid of the words in that statement so that i could just have numbers left? and also there are sometimes particulars numbers i need to sort by so in perl how do you point to which place the number will be since I think the date formatt is constant.

Honestly I am not sure how to approach this problem I may just have my script take an argument of the date that I was to find and then go on from there but it would be really nice if someone could give me an idea of how to extract that date.

Thank you!
# 2  
Old 08-05-2011
Can you post sample of the real data? "This document was accessed 'date' blah blah" is not really useful Smilie
# 3  
Old 08-05-2011
yeah sry bout that. the messages go like this
"Last acessed 2011-06-24 by username at 07:32:55"

Basically right now I just get these files in an array and grep a certain argument like 2011-06. I don't really care about the exact time I just want the date.
# 4  
Old 08-05-2011
If the string is always the same then try this...
Code:
sort -k3,3 -k7,7 file

This User Gave Thanks to shamrock For This Post:
# 5  
Old 08-05-2011
This will sort the filenames in ascending order by date in first line:
Code:
perl -nlae '$F[2]=~s/-//g;$h{$ARGV}=$F[2] if $.==1;$.=0 if eof;END{$,="\n";print sort {$h{$a}<=>$h{$b}} keys %h}' *

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 08-05-2011
Woah thank very much, honestly I have not tried it out yet since I have to integrate that into my script first.

What does the '-nlae' mean, or could you give a brief explanation on what is going on, I understand that ur taking the 3rd argument in the line which happens to be the date then ur doing something with that and then your doing a sort on the results..?

Thank you
# 7  
Old 08-05-2011
I'll explain it after you confirm that it is giving proper results Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Having trouble getting my interactive perl script to work properly

So I'm making an interactive perl script, but I can't get it to work properly. I'm trying to make a script that tell the user to input either 'q' or 'Q' to terminate the program, or 'c' to continue it. If they input anything other than those three keys, it should prompt the user again and again... (5 Replies)
Discussion started by: Eric1
5 Replies

2. Shell Programming and Scripting

Perl REGEX help

Experts - I found a script on one of the servers that I work on and I need help understanding one of the lines. I know what the script does, but I'm having a hard time understanding the grouping. Can someone help me with this? Here's the script... #!/usr/bin/perl use strict; use... (2 Replies)
Discussion started by: timj123
2 Replies

3. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

4. Shell Programming and Scripting

Trouble with alphanumeric Sort

I have a lot of data that need to be sorted alphanumerically. I began using sort -du and it solved almost all my problems. However, when I encountered files with data like this it began to fail: /vol/close_eng_ice_0888 /vol/open_eng_ice_0890 /vol/open_eng_ice_08923 /vol/open_eng_ice_0893... (6 Replies)
Discussion started by: newbie2010
6 Replies

5. Programming

Perl regex

Hi Guys I have the following regex $OSRELEASE = $1 if ($output =~ /(Mac OS X (Server )?10.\d)/); output is currently Mac OS X 10.7.5 when the introduction of Mac 10.8 output changes to OS X 10.8.2 they have dropped the Mac bit so i changed the regex to be (2 Replies)
Discussion started by: ab52
2 Replies

6. Shell Programming and Scripting

Trouble executing piped shell commands in perl code

I am trying to execute a piped combination of shell commands inside a perl program. However, it is not working as desired. This is my program, i am trying to print only filenames from the output of ls -l $ cat list_test #!/usr/bin/perl -w use strict; my $count=0; my @list=`ls -l|awk... (4 Replies)
Discussion started by: sam05121988
4 Replies

7. UNIX for Dummies Questions & Answers

Trouble with grouping regex

Hi Forum im trying to use grouping in a regex statement in a function in a script this is the criteria im trying to match :It MUST have 3 character at the beginning. After that it can have a mix of spaces,alpha-numeric and dashes in any order eg HUG this-stuff, FGU taylor-8-shoes, ZDFnintendo... (2 Replies)
Discussion started by: ShinTec
2 Replies

8. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

9. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies

10. Shell Programming and Scripting

q with Perl Regex

For a programming exercise, I am mean to design a Perl script that detects double letters in a text file. I tried the following expressions # Check for any double letter within the alphabet /+/ # Check for any repetition of an alphanumeric character /\w+/ Im aware that the... (8 Replies)
Discussion started by: JamesGoh
8 Replies
Login or Register to Ask a Question