List files sorted according to specific line of contents

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a directory of files. There is a line in each file that says:
# order: N
where N is an integer number. I want to list all files in the directory (or even include them in wrapper script) according to that N number. Is this possible from a bash command-line?
files sort
add a comment |Â
up vote
2
down vote
favorite
I have a directory of files. There is a line in each file that says:
# order: N
where N is an integer number. I want to list all files in the directory (or even include them in wrapper script) according to that N number. Is this possible from a bash command-line?
files sort
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a directory of files. There is a line in each file that says:
# order: N
where N is an integer number. I want to list all files in the directory (or even include them in wrapper script) according to that N number. Is this possible from a bash command-line?
files sort
I have a directory of files. There is a line in each file that says:
# order: N
where N is an integer number. I want to list all files in the directory (or even include them in wrapper script) according to that N number. Is this possible from a bash command-line?
files sort
edited Nov 29 '17 at 17:22
Jeff Schaller
32.1k849109
32.1k849109
asked Nov 29 '17 at 16:18
user1371264
1365
1365
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
With GNU grep, and assuming file names don't contain colon or newline characters:
$ ls
bar baz foo freeble quux
$ cat ./*
# order: 3
# order: 2
# order: 1
# order: 4
# order: 5
$ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
foo
baz
bar
freeble
quux
Better usegrep -H, otherwise it won't work if there is only one file.
â xenoid
Nov 29 '17 at 16:41
Good catch. Done.
â DopeGhoti
Nov 29 '17 at 16:42
add a comment |Â
up vote
2
down vote
With single GNU awk process:
awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc"
/order: [0-9]+/ a[FILENAME]=$NF; nextfile
END for(i in a) print i ' ./*
add a comment |Â
up vote
2
down vote
With zsh, you can define a glob sorting order based on the content of those lines with:
byOrder() REPLY=$(grep '^# order:' < $REPLY)
and then use it for example with:
printf '%sn' *(.no+byOrder)
or
sorted_file_list=(*(.no+byOrder))
(also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
With GNU grep, and assuming file names don't contain colon or newline characters:
$ ls
bar baz foo freeble quux
$ cat ./*
# order: 3
# order: 2
# order: 1
# order: 4
# order: 5
$ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
foo
baz
bar
freeble
quux
Better usegrep -H, otherwise it won't work if there is only one file.
â xenoid
Nov 29 '17 at 16:41
Good catch. Done.
â DopeGhoti
Nov 29 '17 at 16:42
add a comment |Â
up vote
4
down vote
accepted
With GNU grep, and assuming file names don't contain colon or newline characters:
$ ls
bar baz foo freeble quux
$ cat ./*
# order: 3
# order: 2
# order: 1
# order: 4
# order: 5
$ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
foo
baz
bar
freeble
quux
Better usegrep -H, otherwise it won't work if there is only one file.
â xenoid
Nov 29 '17 at 16:41
Good catch. Done.
â DopeGhoti
Nov 29 '17 at 16:42
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
With GNU grep, and assuming file names don't contain colon or newline characters:
$ ls
bar baz foo freeble quux
$ cat ./*
# order: 3
# order: 2
# order: 1
# order: 4
# order: 5
$ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
foo
baz
bar
freeble
quux
With GNU grep, and assuming file names don't contain colon or newline characters:
$ ls
bar baz foo freeble quux
$ cat ./*
# order: 3
# order: 2
# order: 1
# order: 4
# order: 5
$ grep -m1 -EH '^# order: [0-9]+$' ./* | sort -n -k3 | cut -d: -f1
foo
baz
bar
freeble
quux
edited Nov 29 '17 at 16:58
Stéphane Chazelas
282k53520854
282k53520854
answered Nov 29 '17 at 16:27
DopeGhoti
40.6k54979
40.6k54979
Better usegrep -H, otherwise it won't work if there is only one file.
â xenoid
Nov 29 '17 at 16:41
Good catch. Done.
â DopeGhoti
Nov 29 '17 at 16:42
add a comment |Â
Better usegrep -H, otherwise it won't work if there is only one file.
â xenoid
Nov 29 '17 at 16:41
Good catch. Done.
â DopeGhoti
Nov 29 '17 at 16:42
Better use
grep -H, otherwise it won't work if there is only one file.â xenoid
Nov 29 '17 at 16:41
Better use
grep -H, otherwise it won't work if there is only one file.â xenoid
Nov 29 '17 at 16:41
Good catch. Done.
â DopeGhoti
Nov 29 '17 at 16:42
Good catch. Done.
â DopeGhoti
Nov 29 '17 at 16:42
add a comment |Â
up vote
2
down vote
With single GNU awk process:
awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc"
/order: [0-9]+/ a[FILENAME]=$NF; nextfile
END for(i in a) print i ' ./*
add a comment |Â
up vote
2
down vote
With single GNU awk process:
awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc"
/order: [0-9]+/ a[FILENAME]=$NF; nextfile
END for(i in a) print i ' ./*
add a comment |Â
up vote
2
down vote
up vote
2
down vote
With single GNU awk process:
awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc"
/order: [0-9]+/ a[FILENAME]=$NF; nextfile
END for(i in a) print i ' ./*
With single GNU awk process:
awk 'BEGIN PROCINFO["sorted_in"]="@val_num_asc"
/order: [0-9]+/ a[FILENAME]=$NF; nextfile
END for(i in a) print i ' ./*
edited Nov 29 '17 at 16:53
Stéphane Chazelas
282k53520854
282k53520854
answered Nov 29 '17 at 16:43
RomanPerekhrest
22.4k12145
22.4k12145
add a comment |Â
add a comment |Â
up vote
2
down vote
With zsh, you can define a glob sorting order based on the content of those lines with:
byOrder() REPLY=$(grep '^# order:' < $REPLY)
and then use it for example with:
printf '%sn' *(.no+byOrder)
or
sorted_file_list=(*(.no+byOrder))
(also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).
add a comment |Â
up vote
2
down vote
With zsh, you can define a glob sorting order based on the content of those lines with:
byOrder() REPLY=$(grep '^# order:' < $REPLY)
and then use it for example with:
printf '%sn' *(.no+byOrder)
or
sorted_file_list=(*(.no+byOrder))
(also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).
add a comment |Â
up vote
2
down vote
up vote
2
down vote
With zsh, you can define a glob sorting order based on the content of those lines with:
byOrder() REPLY=$(grep '^# order:' < $REPLY)
and then use it for example with:
printf '%sn' *(.no+byOrder)
or
sorted_file_list=(*(.no+byOrder))
(also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).
With zsh, you can define a glob sorting order based on the content of those lines with:
byOrder() REPLY=$(grep '^# order:' < $REPLY)
and then use it for example with:
printf '%sn' *(.no+byOrder)
or
sorted_file_list=(*(.no+byOrder))
(also adding a . to the glob qualifier to only consider regular files (not directories, fifos, symlinks...)).
edited Nov 29 '17 at 17:01
answered Nov 29 '17 at 16:49
Stéphane Chazelas
282k53520854
282k53520854
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407770%2flist-files-sorted-according-to-specific-line-of-contents%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password