How to process files in pairs
I need to process files in couples.
So if I have:
00000.nii.gz
00001.nii.gz
00002.nii.gz
00003.nii.gz
and so on... (nii.gz
is an extension used during imaging resonance processing) I need to process the file 00000.nii.gz
and 00001.nii.gz
together, then 0002.nii.gz
and 00003.nii.gz
and so on. For each output, I need to rename it.
linux bash scripting
add a comment |
I need to process files in couples.
So if I have:
00000.nii.gz
00001.nii.gz
00002.nii.gz
00003.nii.gz
and so on... (nii.gz
is an extension used during imaging resonance processing) I need to process the file 00000.nii.gz
and 00001.nii.gz
together, then 0002.nii.gz
and 00003.nii.gz
and so on. For each output, I need to rename it.
linux bash scripting
Are these files in a directory or do you have a list of them?
– rusty shackleford
Feb 22 '18 at 10:44
1
What does "couples" mean here? Do you need to handle some sets of files in groups, or just perform the same process on each file individually and independently? For the latter,for f in *; do
, see mywiki.wooledge.org/BashGuide/…
– ilkkachu
Feb 22 '18 at 10:48
The files are in a directory, you can call it just db.dir
– user277297
Feb 22 '18 at 10:51
Bu couple I mean that I need to perform the same process for like 00000.nii.gz and 00001.nii.gz and than the same process for the couple 00002.nii.gz with 0003.nii.gz and so on
– user277297
Feb 22 '18 at 10:53
What renaming would you need to do?
– Kusalananda
Feb 22 '18 at 11:30
add a comment |
I need to process files in couples.
So if I have:
00000.nii.gz
00001.nii.gz
00002.nii.gz
00003.nii.gz
and so on... (nii.gz
is an extension used during imaging resonance processing) I need to process the file 00000.nii.gz
and 00001.nii.gz
together, then 0002.nii.gz
and 00003.nii.gz
and so on. For each output, I need to rename it.
linux bash scripting
I need to process files in couples.
So if I have:
00000.nii.gz
00001.nii.gz
00002.nii.gz
00003.nii.gz
and so on... (nii.gz
is an extension used during imaging resonance processing) I need to process the file 00000.nii.gz
and 00001.nii.gz
together, then 0002.nii.gz
and 00003.nii.gz
and so on. For each output, I need to rename it.
linux bash scripting
linux bash scripting
edited Dec 19 '18 at 23:58
Rui F Ribeiro
39k1479130
39k1479130
asked Feb 22 '18 at 10:43
user277297
32
32
Are these files in a directory or do you have a list of them?
– rusty shackleford
Feb 22 '18 at 10:44
1
What does "couples" mean here? Do you need to handle some sets of files in groups, or just perform the same process on each file individually and independently? For the latter,for f in *; do
, see mywiki.wooledge.org/BashGuide/…
– ilkkachu
Feb 22 '18 at 10:48
The files are in a directory, you can call it just db.dir
– user277297
Feb 22 '18 at 10:51
Bu couple I mean that I need to perform the same process for like 00000.nii.gz and 00001.nii.gz and than the same process for the couple 00002.nii.gz with 0003.nii.gz and so on
– user277297
Feb 22 '18 at 10:53
What renaming would you need to do?
– Kusalananda
Feb 22 '18 at 11:30
add a comment |
Are these files in a directory or do you have a list of them?
– rusty shackleford
Feb 22 '18 at 10:44
1
What does "couples" mean here? Do you need to handle some sets of files in groups, or just perform the same process on each file individually and independently? For the latter,for f in *; do
, see mywiki.wooledge.org/BashGuide/…
– ilkkachu
Feb 22 '18 at 10:48
The files are in a directory, you can call it just db.dir
– user277297
Feb 22 '18 at 10:51
Bu couple I mean that I need to perform the same process for like 00000.nii.gz and 00001.nii.gz and than the same process for the couple 00002.nii.gz with 0003.nii.gz and so on
– user277297
Feb 22 '18 at 10:53
What renaming would you need to do?
– Kusalananda
Feb 22 '18 at 11:30
Are these files in a directory or do you have a list of them?
– rusty shackleford
Feb 22 '18 at 10:44
Are these files in a directory or do you have a list of them?
– rusty shackleford
Feb 22 '18 at 10:44
1
1
What does "couples" mean here? Do you need to handle some sets of files in groups, or just perform the same process on each file individually and independently? For the latter,
for f in *; do
, see mywiki.wooledge.org/BashGuide/…– ilkkachu
Feb 22 '18 at 10:48
What does "couples" mean here? Do you need to handle some sets of files in groups, or just perform the same process on each file individually and independently? For the latter,
for f in *; do
, see mywiki.wooledge.org/BashGuide/…– ilkkachu
Feb 22 '18 at 10:48
The files are in a directory, you can call it just db.dir
– user277297
Feb 22 '18 at 10:51
The files are in a directory, you can call it just db.dir
– user277297
Feb 22 '18 at 10:51
Bu couple I mean that I need to perform the same process for like 00000.nii.gz and 00001.nii.gz and than the same process for the couple 00002.nii.gz with 0003.nii.gz and so on
– user277297
Feb 22 '18 at 10:53
Bu couple I mean that I need to perform the same process for like 00000.nii.gz and 00001.nii.gz and than the same process for the couple 00002.nii.gz with 0003.nii.gz and so on
– user277297
Feb 22 '18 at 10:53
What renaming would you need to do?
– Kusalananda
Feb 22 '18 at 11:30
What renaming would you need to do?
– Kusalananda
Feb 22 '18 at 11:30
add a comment |
2 Answers
2
active
oldest
votes
I'm assuming that the number in the file names are five-digit zero-filled numbers and that the occasional four-digit string in the question and in the comments are typos.
#!/bin/bash
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf '%05d.nii.gz' "$i" )
name2=$( printf '%05d.nii.gz' "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
The loop goes from zero to a large number in increments of two. For each iteration, it constructs the two filenames using the variable i
in two calls to printf
(once for i
and once for i+1
). The format specifier %05d
formats an integer to a zero-filled five-digit string.
As soon as the constructed filenames no longer corresponds to any existing files (in the current directory), the loop exits.
If the files are located in another directory than in the current, change the printf
format string from '%05d.nii.gz'
to '/path/to/dir/%05d.nii.gz'
.
To keep with the DRY principle ("Don't Repeat Yourself"):
#!/bin/bash
dir='/path/to/dir'
name_fmt="$dir/%05d.nii.gz"
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf "$name_fmt" "$i" )
name2=$( printf "$name_fmt" "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
Thx for the help, I need to try this script. by the way, the files are named like this: dr_stage2_subject00000.nii.gz , then dr_stage2_subject00001.nii.gz and so on
– user277297
Feb 22 '18 at 12:09
@user277297 Then changename_fmt
in my last code piece todr_stage2_subject%05d.nii.gz
(possibly with a directory path in front).
– Kusalananda
Feb 22 '18 at 12:12
#!/bin/bash dir='/project/3013056.01/Test/dual_regression.dr' name_fmt="$dir/dr_stage2_subject%05d.nii.gz" for (( i = 0; i <= 99999; i += 2 )); do name1=$( printf "$dr_stage2_subject%05d.nii.gz" "$i" ) name2=$( printf "$dr_stage2_subject%05d.nii.gz" "$(( i + 1 ))" ) if [ -f "$name1" ] && [ -f "$name2" ]; then printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2 fslmaths $name1 -sub $name2 else printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2 break fi done
– user277297
Feb 22 '18 at 12:23
But it gives me this error"00000.nii.gz" or "00001.nii.gz" not found, exiting.
– user277297
Feb 22 '18 at 12:25
@user277297 You have combined my two solutions. Only look at the second one if that makes it easier. The twoprintf
calls that generate the filenames should look exactly like I have them in my code, using$name_fmt
and not$dr_...
(you are inserting a nonexistent variable here, which is why it doesn't work for you).
– Kusalananda
Feb 22 '18 at 12:29
|
show 8 more comments
Suppose you had a number of files like this:
echo z?
z1 z2 z3 z4 z5 z6 z7 z8 z9
Then to get couples, you could use:
echo z? | xargs -n2
z1 z2
z3 z4
z5 z6
z7 z8
z9
To get triples:
echo z? | xargs -n3
z1 z2 z3
z4 z5 z6
z7 z8 z9
The default command for xargs is echo, but you can supply your own command. The man page for xargs, while long, includes a few examples at the end.
Best wishes ... cheers, drl
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f425863%2fhow-to-process-files-in-pairs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm assuming that the number in the file names are five-digit zero-filled numbers and that the occasional four-digit string in the question and in the comments are typos.
#!/bin/bash
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf '%05d.nii.gz' "$i" )
name2=$( printf '%05d.nii.gz' "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
The loop goes from zero to a large number in increments of two. For each iteration, it constructs the two filenames using the variable i
in two calls to printf
(once for i
and once for i+1
). The format specifier %05d
formats an integer to a zero-filled five-digit string.
As soon as the constructed filenames no longer corresponds to any existing files (in the current directory), the loop exits.
If the files are located in another directory than in the current, change the printf
format string from '%05d.nii.gz'
to '/path/to/dir/%05d.nii.gz'
.
To keep with the DRY principle ("Don't Repeat Yourself"):
#!/bin/bash
dir='/path/to/dir'
name_fmt="$dir/%05d.nii.gz"
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf "$name_fmt" "$i" )
name2=$( printf "$name_fmt" "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
Thx for the help, I need to try this script. by the way, the files are named like this: dr_stage2_subject00000.nii.gz , then dr_stage2_subject00001.nii.gz and so on
– user277297
Feb 22 '18 at 12:09
@user277297 Then changename_fmt
in my last code piece todr_stage2_subject%05d.nii.gz
(possibly with a directory path in front).
– Kusalananda
Feb 22 '18 at 12:12
#!/bin/bash dir='/project/3013056.01/Test/dual_regression.dr' name_fmt="$dir/dr_stage2_subject%05d.nii.gz" for (( i = 0; i <= 99999; i += 2 )); do name1=$( printf "$dr_stage2_subject%05d.nii.gz" "$i" ) name2=$( printf "$dr_stage2_subject%05d.nii.gz" "$(( i + 1 ))" ) if [ -f "$name1" ] && [ -f "$name2" ]; then printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2 fslmaths $name1 -sub $name2 else printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2 break fi done
– user277297
Feb 22 '18 at 12:23
But it gives me this error"00000.nii.gz" or "00001.nii.gz" not found, exiting.
– user277297
Feb 22 '18 at 12:25
@user277297 You have combined my two solutions. Only look at the second one if that makes it easier. The twoprintf
calls that generate the filenames should look exactly like I have them in my code, using$name_fmt
and not$dr_...
(you are inserting a nonexistent variable here, which is why it doesn't work for you).
– Kusalananda
Feb 22 '18 at 12:29
|
show 8 more comments
I'm assuming that the number in the file names are five-digit zero-filled numbers and that the occasional four-digit string in the question and in the comments are typos.
#!/bin/bash
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf '%05d.nii.gz' "$i" )
name2=$( printf '%05d.nii.gz' "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
The loop goes from zero to a large number in increments of two. For each iteration, it constructs the two filenames using the variable i
in two calls to printf
(once for i
and once for i+1
). The format specifier %05d
formats an integer to a zero-filled five-digit string.
As soon as the constructed filenames no longer corresponds to any existing files (in the current directory), the loop exits.
If the files are located in another directory than in the current, change the printf
format string from '%05d.nii.gz'
to '/path/to/dir/%05d.nii.gz'
.
To keep with the DRY principle ("Don't Repeat Yourself"):
#!/bin/bash
dir='/path/to/dir'
name_fmt="$dir/%05d.nii.gz"
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf "$name_fmt" "$i" )
name2=$( printf "$name_fmt" "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
Thx for the help, I need to try this script. by the way, the files are named like this: dr_stage2_subject00000.nii.gz , then dr_stage2_subject00001.nii.gz and so on
– user277297
Feb 22 '18 at 12:09
@user277297 Then changename_fmt
in my last code piece todr_stage2_subject%05d.nii.gz
(possibly with a directory path in front).
– Kusalananda
Feb 22 '18 at 12:12
#!/bin/bash dir='/project/3013056.01/Test/dual_regression.dr' name_fmt="$dir/dr_stage2_subject%05d.nii.gz" for (( i = 0; i <= 99999; i += 2 )); do name1=$( printf "$dr_stage2_subject%05d.nii.gz" "$i" ) name2=$( printf "$dr_stage2_subject%05d.nii.gz" "$(( i + 1 ))" ) if [ -f "$name1" ] && [ -f "$name2" ]; then printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2 fslmaths $name1 -sub $name2 else printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2 break fi done
– user277297
Feb 22 '18 at 12:23
But it gives me this error"00000.nii.gz" or "00001.nii.gz" not found, exiting.
– user277297
Feb 22 '18 at 12:25
@user277297 You have combined my two solutions. Only look at the second one if that makes it easier. The twoprintf
calls that generate the filenames should look exactly like I have them in my code, using$name_fmt
and not$dr_...
(you are inserting a nonexistent variable here, which is why it doesn't work for you).
– Kusalananda
Feb 22 '18 at 12:29
|
show 8 more comments
I'm assuming that the number in the file names are five-digit zero-filled numbers and that the occasional four-digit string in the question and in the comments are typos.
#!/bin/bash
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf '%05d.nii.gz' "$i" )
name2=$( printf '%05d.nii.gz' "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
The loop goes from zero to a large number in increments of two. For each iteration, it constructs the two filenames using the variable i
in two calls to printf
(once for i
and once for i+1
). The format specifier %05d
formats an integer to a zero-filled five-digit string.
As soon as the constructed filenames no longer corresponds to any existing files (in the current directory), the loop exits.
If the files are located in another directory than in the current, change the printf
format string from '%05d.nii.gz'
to '/path/to/dir/%05d.nii.gz'
.
To keep with the DRY principle ("Don't Repeat Yourself"):
#!/bin/bash
dir='/path/to/dir'
name_fmt="$dir/%05d.nii.gz"
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf "$name_fmt" "$i" )
name2=$( printf "$name_fmt" "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
I'm assuming that the number in the file names are five-digit zero-filled numbers and that the occasional four-digit string in the question and in the comments are typos.
#!/bin/bash
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf '%05d.nii.gz' "$i" )
name2=$( printf '%05d.nii.gz' "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
The loop goes from zero to a large number in increments of two. For each iteration, it constructs the two filenames using the variable i
in two calls to printf
(once for i
and once for i+1
). The format specifier %05d
formats an integer to a zero-filled five-digit string.
As soon as the constructed filenames no longer corresponds to any existing files (in the current directory), the loop exits.
If the files are located in another directory than in the current, change the printf
format string from '%05d.nii.gz'
to '/path/to/dir/%05d.nii.gz'
.
To keep with the DRY principle ("Don't Repeat Yourself"):
#!/bin/bash
dir='/path/to/dir'
name_fmt="$dir/%05d.nii.gz"
for (( i = 0; i <= 99999; i += 2 )); do
name1=$( printf "$name_fmt" "$i" )
name2=$( printf "$name_fmt" "$(( i + 1 ))" )
if [ -f "$name1" ] && [ -f "$name2" ]; then
printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2
# use the two files here
else
printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2
break
fi
done
edited Feb 22 '18 at 11:35
answered Feb 22 '18 at 11:13
Kusalananda
121k16229372
121k16229372
Thx for the help, I need to try this script. by the way, the files are named like this: dr_stage2_subject00000.nii.gz , then dr_stage2_subject00001.nii.gz and so on
– user277297
Feb 22 '18 at 12:09
@user277297 Then changename_fmt
in my last code piece todr_stage2_subject%05d.nii.gz
(possibly with a directory path in front).
– Kusalananda
Feb 22 '18 at 12:12
#!/bin/bash dir='/project/3013056.01/Test/dual_regression.dr' name_fmt="$dir/dr_stage2_subject%05d.nii.gz" for (( i = 0; i <= 99999; i += 2 )); do name1=$( printf "$dr_stage2_subject%05d.nii.gz" "$i" ) name2=$( printf "$dr_stage2_subject%05d.nii.gz" "$(( i + 1 ))" ) if [ -f "$name1" ] && [ -f "$name2" ]; then printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2 fslmaths $name1 -sub $name2 else printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2 break fi done
– user277297
Feb 22 '18 at 12:23
But it gives me this error"00000.nii.gz" or "00001.nii.gz" not found, exiting.
– user277297
Feb 22 '18 at 12:25
@user277297 You have combined my two solutions. Only look at the second one if that makes it easier. The twoprintf
calls that generate the filenames should look exactly like I have them in my code, using$name_fmt
and not$dr_...
(you are inserting a nonexistent variable here, which is why it doesn't work for you).
– Kusalananda
Feb 22 '18 at 12:29
|
show 8 more comments
Thx for the help, I need to try this script. by the way, the files are named like this: dr_stage2_subject00000.nii.gz , then dr_stage2_subject00001.nii.gz and so on
– user277297
Feb 22 '18 at 12:09
@user277297 Then changename_fmt
in my last code piece todr_stage2_subject%05d.nii.gz
(possibly with a directory path in front).
– Kusalananda
Feb 22 '18 at 12:12
#!/bin/bash dir='/project/3013056.01/Test/dual_regression.dr' name_fmt="$dir/dr_stage2_subject%05d.nii.gz" for (( i = 0; i <= 99999; i += 2 )); do name1=$( printf "$dr_stage2_subject%05d.nii.gz" "$i" ) name2=$( printf "$dr_stage2_subject%05d.nii.gz" "$(( i + 1 ))" ) if [ -f "$name1" ] && [ -f "$name2" ]; then printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2 fslmaths $name1 -sub $name2 else printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2 break fi done
– user277297
Feb 22 '18 at 12:23
But it gives me this error"00000.nii.gz" or "00001.nii.gz" not found, exiting.
– user277297
Feb 22 '18 at 12:25
@user277297 You have combined my two solutions. Only look at the second one if that makes it easier. The twoprintf
calls that generate the filenames should look exactly like I have them in my code, using$name_fmt
and not$dr_...
(you are inserting a nonexistent variable here, which is why it doesn't work for you).
– Kusalananda
Feb 22 '18 at 12:29
Thx for the help, I need to try this script. by the way, the files are named like this: dr_stage2_subject00000.nii.gz , then dr_stage2_subject00001.nii.gz and so on
– user277297
Feb 22 '18 at 12:09
Thx for the help, I need to try this script. by the way, the files are named like this: dr_stage2_subject00000.nii.gz , then dr_stage2_subject00001.nii.gz and so on
– user277297
Feb 22 '18 at 12:09
@user277297 Then change
name_fmt
in my last code piece to dr_stage2_subject%05d.nii.gz
(possibly with a directory path in front).– Kusalananda
Feb 22 '18 at 12:12
@user277297 Then change
name_fmt
in my last code piece to dr_stage2_subject%05d.nii.gz
(possibly with a directory path in front).– Kusalananda
Feb 22 '18 at 12:12
#!/bin/bash dir='/project/3013056.01/Test/dual_regression.dr' name_fmt="$dir/dr_stage2_subject%05d.nii.gz" for (( i = 0; i <= 99999; i += 2 )); do name1=$( printf "$dr_stage2_subject%05d.nii.gz" "$i" ) name2=$( printf "$dr_stage2_subject%05d.nii.gz" "$(( i + 1 ))" ) if [ -f "$name1" ] && [ -f "$name2" ]; then printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2 fslmaths $name1 -sub $name2 else printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2 break fi done
– user277297
Feb 22 '18 at 12:23
#!/bin/bash dir='/project/3013056.01/Test/dual_regression.dr' name_fmt="$dir/dr_stage2_subject%05d.nii.gz" for (( i = 0; i <= 99999; i += 2 )); do name1=$( printf "$dr_stage2_subject%05d.nii.gz" "$i" ) name2=$( printf "$dr_stage2_subject%05d.nii.gz" "$(( i + 1 ))" ) if [ -f "$name1" ] && [ -f "$name2" ]; then printf 'Processing "%s" and "%s" together...n' "$name1" "$name2" >&2 fslmaths $name1 -sub $name2 else printf '"%s" or "%s" not found, exiting.n' "$name1" "$name2" >&2 break fi done
– user277297
Feb 22 '18 at 12:23
But it gives me this error"00000.nii.gz" or "00001.nii.gz" not found, exiting.
– user277297
Feb 22 '18 at 12:25
But it gives me this error"00000.nii.gz" or "00001.nii.gz" not found, exiting.
– user277297
Feb 22 '18 at 12:25
@user277297 You have combined my two solutions. Only look at the second one if that makes it easier. The two
printf
calls that generate the filenames should look exactly like I have them in my code, using $name_fmt
and not $dr_...
(you are inserting a nonexistent variable here, which is why it doesn't work for you).– Kusalananda
Feb 22 '18 at 12:29
@user277297 You have combined my two solutions. Only look at the second one if that makes it easier. The two
printf
calls that generate the filenames should look exactly like I have them in my code, using $name_fmt
and not $dr_...
(you are inserting a nonexistent variable here, which is why it doesn't work for you).– Kusalananda
Feb 22 '18 at 12:29
|
show 8 more comments
Suppose you had a number of files like this:
echo z?
z1 z2 z3 z4 z5 z6 z7 z8 z9
Then to get couples, you could use:
echo z? | xargs -n2
z1 z2
z3 z4
z5 z6
z7 z8
z9
To get triples:
echo z? | xargs -n3
z1 z2 z3
z4 z5 z6
z7 z8 z9
The default command for xargs is echo, but you can supply your own command. The man page for xargs, while long, includes a few examples at the end.
Best wishes ... cheers, drl
add a comment |
Suppose you had a number of files like this:
echo z?
z1 z2 z3 z4 z5 z6 z7 z8 z9
Then to get couples, you could use:
echo z? | xargs -n2
z1 z2
z3 z4
z5 z6
z7 z8
z9
To get triples:
echo z? | xargs -n3
z1 z2 z3
z4 z5 z6
z7 z8 z9
The default command for xargs is echo, but you can supply your own command. The man page for xargs, while long, includes a few examples at the end.
Best wishes ... cheers, drl
add a comment |
Suppose you had a number of files like this:
echo z?
z1 z2 z3 z4 z5 z6 z7 z8 z9
Then to get couples, you could use:
echo z? | xargs -n2
z1 z2
z3 z4
z5 z6
z7 z8
z9
To get triples:
echo z? | xargs -n3
z1 z2 z3
z4 z5 z6
z7 z8 z9
The default command for xargs is echo, but you can supply your own command. The man page for xargs, while long, includes a few examples at the end.
Best wishes ... cheers, drl
Suppose you had a number of files like this:
echo z?
z1 z2 z3 z4 z5 z6 z7 z8 z9
Then to get couples, you could use:
echo z? | xargs -n2
z1 z2
z3 z4
z5 z6
z7 z8
z9
To get triples:
echo z? | xargs -n3
z1 z2 z3
z4 z5 z6
z7 z8 z9
The default command for xargs is echo, but you can supply your own command. The man page for xargs, while long, includes a few examples at the end.
Best wishes ... cheers, drl
answered Feb 22 '18 at 12:14
drl
46225
46225
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f425863%2fhow-to-process-files-in-pairs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are these files in a directory or do you have a list of them?
– rusty shackleford
Feb 22 '18 at 10:44
1
What does "couples" mean here? Do you need to handle some sets of files in groups, or just perform the same process on each file individually and independently? For the latter,
for f in *; do
, see mywiki.wooledge.org/BashGuide/…– ilkkachu
Feb 22 '18 at 10:48
The files are in a directory, you can call it just db.dir
– user277297
Feb 22 '18 at 10:51
Bu couple I mean that I need to perform the same process for like 00000.nii.gz and 00001.nii.gz and than the same process for the couple 00002.nii.gz with 0003.nii.gz and so on
– user277297
Feb 22 '18 at 10:53
What renaming would you need to do?
– Kusalananda
Feb 22 '18 at 11:30