tags:

views:

305

answers:

7

Hi,

with a previous bash script I created a list of files:

data_1_box
data_2_box
...
data_10_box
...
data_99_box

the thing is that now I need to concatenate them, so I tried

ls -l data_*

but I get

.....
data_89_box
data_8_box
data_90_box
...
data_99_box
data_9_box

but I need to get in the sucession 1, 2, 3, 4, .. 9, ..., 89, 90, 91, ..., 99

Can it be done in bash?

A: 

This is a generic answer! You have to apply rules to the specific set of data

ls | sort

Example:

ls | sort -n -t _ -k 2
Peter Lindqvist
-1 ?I changed to -l, but I do not get the results in that order
Werner
Did you actually *try* that?
paxdiablo
if a tryls -1 | sort -nI get ...data_89_boxdata_90_box...data_99_boxdata_9_box(end)but i would like them in sequential order
Werner
well the _actual_ is in puppes answer, this was more of a hint to what you need to use.
Peter Lindqvist
I amended my answer to reflect that it was generic in nature.
Peter Lindqvist
+4  A: 
ls data_* | sort -n -t _ -k 2

-n: sorts numerically
-t: field separator '_'
-k: sort on second field, in your case the numbers after the first '_'

Puppe
yes!! that did the trick, thanks!
Werner
A: 

If your sort has version sort, try:

ls -1 | sort -V

(that's a capital V).

Dennis Williamson
+2  A: 

How about using the -v flag to ls? The purpose of the flag is to sort files according to version number, but it works just as well here and eliminates the need to pipe the result to sort:

ls -lv data_*
Pär Wieslander
A: 

One suggestion I can think of is this :

for i in `seq 1 5`
do  
   cat "data_${i}_box"
done
Saravanan
A: 

Here's the way to do it in bash if your sort doesn't have version sort:

cat | awk ' BEGIN { FS="" } { printf( "%03d\n",$2) }' | sort | awk ' { printf( "data%d_box\n", $1) }'

All in one line. Keep in mind, I haven't tested this on your specific data, so it might need a little tweaking to work correctly for you. This outlines a good, robust and relatively simple solution, though. Of course, you can always swap the cat+filename in the beginning with an the actual ls to create the file data on the fly. For capturing the actual filename column, you can choose between correct ls parameters or piping through either cut or awk.

Willy-André Daniel
A: 

maybe you'll like SistemaNumeri.py ("fix numbers"): it renames your

data_1_box
data_2_box
...
data_10_box
...
data_99_box

in

data_01_box
data_02_box
...
data_10_box
...
data_99_box
Vito De Tullio