unix command for terminal - view sequences as a single line?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users unix command for terminal - view sequences as a single line?
# 1  
Old 06-22-2011
unix command for terminal - view sequences as a single line?

Hello,

I'm looking for some code that will list sequences of files as a single line.

ie, sequences of files like this:

filename.1.ext
filename.2.ext
filename.3.ext
filename.4.ext
filename.5.ext
filename.6.ext
filename.7.ext
filename.8.ext
filename.9.ext
filename.10.ext

would be listed as such:

filename.1-10.ext

-

non-sequenced files would just be listed as usual along with the seq... so this group:

filenameA.1.ext filenameB.1.ext
filenameA.2.ext filenameB.2.ext
filenameA.3.ext filenameB.3.ext
filenameA.4.ext filenameB.4.ext
filenameA.5.ext filenameB.5.ext
filenameA.6.ext filenameB.6.ext
filenameA.7.ext filenameB.7.ext
filenameA.8.ext filenameB.8.ext
filenameA.9.ext filenameB.9.ext
filenameA.10.ext filenameB.10.ext filenameC.1.ext

would list as such:

filenameA.1-10.ext
filenameB.1-10.ext
filenameC.1.ext

This will have to function in the OSX & Lunix terminals.

Every company I've been at so far has had some version of this command but now I find myself at a place that doesn't and oh how I do miss it. Anything appreciated, feel free to mail me directly.

Thanks!

Kent
# 2  
Old 06-22-2011
Try:
Code:
ls | perl -F"\." -nae 'push @{$a{$F[0]}},$F[1];END{for $i (keys %a){@n=sort {$a <=> $b}@{$a{$i}};print "$i.$n[0]-$n[$#n].$F[2]"}}'

# 3  
Old 06-22-2011
Thanks!

For these files:

ls
contacts.1.txt contacts.2.txt contacts.3.txt contacts.txt icc.txt ideas.txt notes (Autosaved) notes.txt shownotes_001.txt shownotes_AllShowsShotList_001.txt

that gives me these results:

ls | perl -F"\." -nae 'push @{$a{$F[0]}},$F[1];END{for $i (keys %a){@n=sort {$a <=> $b}@{$a{$i}};print "$i.$n[0]-$n[$#n].$F[2]"}}'

contacts.txt
-3.notes (Autosaved)
.-.icc.txt
-txt
.shownotes_AllShowsShotList_001.txt
-txt
.notes.txt
-txt
.shownotes_001.txt
-txt
.ideas.txt
-txt
.~/notes/ :


What I really am looking for would be this:

contacts.1-3.txt
contacts.txt
icc.txt
ideas.txt
notes (Autosaved)
notes.txt
shownotes_001.txt
shownotes_AllShowsShotList_001.txt


I found another script:

#!/usr/bin/perl
use strict;

#
# lfrms - list frame ranges
# 1.00 01/12/98 erco@3dsite.com
#

$| = 1;

# MAIN
{
my $dir;
my @curdir = ( "." );
my @dirs = ( $#ARGV == -1 ) ? @curdir : @ARGV;

foreach $dir ( @dirs )
{
if ( $#dirs > 0 ) { print "$dir:\n"; }
my @list = split(/\n/, `ls $dir`);
my @last = "";
my $start;
my $pending = 0;

foreach ( @list )
{
if ( /(.*)\.([0-9]*)\.(.*)/ )
{
# NO MATCH ON LAST NAME? SKIP
if ( $1 ne $last[0] || $3 ne $last[2] || ($2-$last[1]) != 1 )
{
# END LAST RANGE
if ( $last[0] ne "" && $last[2] ne "" )
{
if ( $start == $last[1] )
# JUST ONE FRAME? PRINT IT
{ printf("%s.%s\n", $start, $last[2]); }
else
# RANGE? PRINT AS RANGE
{ printf("[%s-%s].%s\n", $start, $last[1], $last[2]); }
}

# START NEW RANGE
printf("%s.", $1);
$start = $2;
$pending = 1;
}

# SAVE FOR NEXT ITER
$last[0] = $1; $last[1] = $2; $last[2] = $3;
}
}
if ( $pending )
{
if ( $start == $last[1] )
# JUST ONE FRAME? PRINT IT
{ printf("%s.%s\n", $start, $last[2]); }
else
# RANGE? PRINT AS RANGE
{ printf("[%s-%s].%s\n", $start, $last[1], $last[2]); }
}
}
}


That returns this:

contacts.[1-3].txt


Which is close to how I'd like the files listed but unfortunately drops the non-sequential files. Would it be easy to modify that command to include the non-sequential files?

Thanks again for offering that.
# 4  
Old 06-22-2011
Try:
Code:
ls | perl -F"\." -nae 'if ($F[2]){push @{$a{$F[0]}},$F[1];$e{$F[0]}=$F[2]}else{$a{$_}=1};END{for $i (keys %a){@n=sort {$a <=> $b}@{$a{$i}};if (@n){print "$i.$n[0]-$n[$#n].$e{$i}"}else{print $i}}}'

# 5  
Old 06-22-2011
Thank you! This is great, does exactly what I need.

I'm having a problem getting it into action though. In my .alias file though, I get a message about an 'unmatched parenthesis' when I source the file, and if I make it into a command .csh file in the /bin dir I get a message about a misplaced ' .

The sorting on the non-sequential files is non-alphabetical though... but of course thats a minor thing.

I'll work it a bit to see if I can get it into my .alias file or into .csh file.

Thanks very much!
# 6  
Old 06-22-2011
Quote:
Originally Posted by kentm
Thank you! This is great, does exactly what I need.

I'm having a problem getting it into action though. In my .alias file though, I get a message about an 'unmatched parenthesis' when I source the file, and if I make it into a command .csh file in the /bin dir I get a message about a misplaced ' .
There are a lot of unsolvable quoting problems in csh, among many other flaws, to the point diatribes have been written about it. Its inventor admits he "wasn't too good at programming" when he wrote it. What if you put it in a bourne script with #!/bin/sh instead?
# 7  
Old 06-28-2011
I haven't tried that, but there is unfortunately a bigger problem with it in that if there are breaks in a sequence this cmd won't show them. For example if there is a range of files like this:


filename.1.ext
filename.2.ext
filename.3.ext
filename.4.ext

filename.6.ext
filename.7.ext
filename.8.ext

then the result is still

filename.1-8.ext

which is not useful. It would need to read out as such:

filename.1-4.ext
filename.6-8.ext

-

Regarding getting it to a form where I can link a shortened cmd to is, someone else suggested this:

"The simplest thing to do is toss that one-liner into an executable file, and modify your alias to pipe the output of ls into that. The next simplest thing to do is switch to bash (assuming you're not using it already), and put it into a function. Otherwise, put a ' (single quote) at the start, and every time you get to a single quote, end the previous quote, wrap the single quote in double quotes, and then start with another single quote:

'ls | perl -F"\\." -nae '"'"'if...

You might consider replacing the internal double quotes with qq(), too."
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. OS X (Apple)

A new OSX 10.12.x terminal from the command line.

Hi guys and gals... After much searching on the good ol' internet I could find nothing, so this is the result. ALthough many people seem to have asked this question no-one seems to have a solution so here we go. I need for AudioScope.sh, 'xterm' to run a second program for some of its... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

3. Shell Programming and Scripting

Can I view doc files from terminal

Sometimes I'm working from terminal for long periods of time and I come across a doc file I don't recognize. I've been trying to figure out how to view it instead of opening up word. I've tried cat but it jumps into binary and my terminal goes nuts. Is there a way I can do this from... (4 Replies)
Discussion started by: syregnar86
4 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. UNIX for Advanced & Expert Users

View tiled photo thumbnails in command line

Is there a good way of showing around 4 pictures (fairly large thumbnails) at once on the command line, preferably tiled so that all of them are viewable. I tried using display but I'm not sure how to tile them. I'd also prefer if it was read only so that they cannot be altered in the viewer. ... (1 Reply)
Discussion started by: cue
1 Replies

6. OS X (Apple)

How to start a new terminal from command line?

Dear All, Anyone knows how to start a new bash terminal from command line? Another question: when I use "open" command (open test.pdf) to open a pdf file, the PDF reader will start up, but cannot associate with that file. Anyone knows why? (1 Reply)
Discussion started by: andrewust
1 Replies

7. UNIX Desktop Questions & Answers

How do you reverse terminal command line to the top?

Hi All, I work on a Linux platform which runs Red Hat (forget which version) and use both korn and bash shells. Is there a way of making the command line appear at the top of the terminal window and any lists, commands or directory names etc to appear below the top, that is to say reverse the... (1 Reply)
Discussion started by: ray_m
1 Replies

8. Shell Programming and Scripting

Is command line invocation of gnome-terminal to run more than one command possible?

Hello, I am trying to learn how to pass something more than a one-command startup for gnome-terminal. I will give an example of what I'm trying to do here: #! /bin/bash # #TODO write this for gnome and xterm USAGE=" ______________________________________________ ${0##*/} run... (0 Replies)
Discussion started by: Narnie
0 Replies

9. UNIX for Dummies Questions & Answers

run command Unix on a single line

Hi everybody.. I need to enter in bash mode and then run a command and this just in a single command line. I tried : "bash ^M| somecommand" but nothing.. How do I do to simulate the return button just right after the bash command ? Thanks.. (8 Replies)
Discussion started by: Riddick61
8 Replies
Login or Register to Ask a Question