How to access file-name's ending (before the extension) in a loop
up vote
0
down vote
favorite
I have a folder with file-names ending .TIF
, and I did a for loop
for i in *.TIF
In the loop, I want to get two types of files ending for example (B4.TIF and B5.TIF.
I did it like this but, it doesn't get the last four letters of the file name, instead it adds them at the end.
do ...... ${i}_B4.TIF -B ${i}_B5.TIF
My desired results are to get those file names in order to do some calculations
these are the examples of the files in the folder
LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF
LC08_L1TP_195020_20171107_20171121_01_T1_B4.TIF
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
LC08_L1TP_195020_20171107_20171121_01_T1_B5.TIF
and this is the script I am using to do the calculations
for i in *.TIF
do
gdal_calc.py -A ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF --type=Float32
--outfile=~/Geodata/goteborg/croped/ndvi/${i}_NDVI.TIF --calc="(B-A)/(B+A)"
done
The final output will be subtracting the B4 and B5. so this ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF
should get the two file names like this:
LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF -
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
bash
New contributor
|
show 2 more comments
up vote
0
down vote
favorite
I have a folder with file-names ending .TIF
, and I did a for loop
for i in *.TIF
In the loop, I want to get two types of files ending for example (B4.TIF and B5.TIF.
I did it like this but, it doesn't get the last four letters of the file name, instead it adds them at the end.
do ...... ${i}_B4.TIF -B ${i}_B5.TIF
My desired results are to get those file names in order to do some calculations
these are the examples of the files in the folder
LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF
LC08_L1TP_195020_20171107_20171121_01_T1_B4.TIF
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
LC08_L1TP_195020_20171107_20171121_01_T1_B5.TIF
and this is the script I am using to do the calculations
for i in *.TIF
do
gdal_calc.py -A ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF --type=Float32
--outfile=~/Geodata/goteborg/croped/ndvi/${i}_NDVI.TIF --calc="(B-A)/(B+A)"
done
The final output will be subtracting the B4 and B5. so this ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF
should get the two file names like this:
LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF -
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
bash
New contributor
Please list an example input and output (just a few lines).
– ctrl-alt-delor
Nov 14 at 10:20
1
Why don't you loop over./*B[45]_SA.TIF
? Or do you need both files i the same iteration of the loop. Please explain more about what it is you're doing.
– Kusalananda
Nov 14 at 10:23
I need both files in the same iteration. see the example and the script in my last edit
– MKJ
Nov 14 at 10:24
I don't see an_SA
in the examples, is this the problem.
– ctrl-alt-delor
Nov 14 at 10:27
Your example command takes two inputs and produces one output, but your list of example files doesn't have any "matches" between theB4
andB5
set. Please indicate which pairs of input files you want to use together, and how you decide the output filename each time.
– JigglyNaga
Nov 14 at 10:30
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a folder with file-names ending .TIF
, and I did a for loop
for i in *.TIF
In the loop, I want to get two types of files ending for example (B4.TIF and B5.TIF.
I did it like this but, it doesn't get the last four letters of the file name, instead it adds them at the end.
do ...... ${i}_B4.TIF -B ${i}_B5.TIF
My desired results are to get those file names in order to do some calculations
these are the examples of the files in the folder
LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF
LC08_L1TP_195020_20171107_20171121_01_T1_B4.TIF
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
LC08_L1TP_195020_20171107_20171121_01_T1_B5.TIF
and this is the script I am using to do the calculations
for i in *.TIF
do
gdal_calc.py -A ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF --type=Float32
--outfile=~/Geodata/goteborg/croped/ndvi/${i}_NDVI.TIF --calc="(B-A)/(B+A)"
done
The final output will be subtracting the B4 and B5. so this ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF
should get the two file names like this:
LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF -
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
bash
New contributor
I have a folder with file-names ending .TIF
, and I did a for loop
for i in *.TIF
In the loop, I want to get two types of files ending for example (B4.TIF and B5.TIF.
I did it like this but, it doesn't get the last four letters of the file name, instead it adds them at the end.
do ...... ${i}_B4.TIF -B ${i}_B5.TIF
My desired results are to get those file names in order to do some calculations
these are the examples of the files in the folder
LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF
LC08_L1TP_195020_20171107_20171121_01_T1_B4.TIF
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
LC08_L1TP_195020_20171107_20171121_01_T1_B5.TIF
and this is the script I am using to do the calculations
for i in *.TIF
do
gdal_calc.py -A ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF --type=Float32
--outfile=~/Geodata/goteborg/croped/ndvi/${i}_NDVI.TIF --calc="(B-A)/(B+A)"
done
The final output will be subtracting the B4 and B5. so this ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF
should get the two file names like this:
LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF -
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
bash
bash
New contributor
New contributor
edited Nov 14 at 10:41
ctrl-alt-delor
9,87031954
9,87031954
New contributor
asked Nov 14 at 10:16
MKJ
12
12
New contributor
New contributor
Please list an example input and output (just a few lines).
– ctrl-alt-delor
Nov 14 at 10:20
1
Why don't you loop over./*B[45]_SA.TIF
? Or do you need both files i the same iteration of the loop. Please explain more about what it is you're doing.
– Kusalananda
Nov 14 at 10:23
I need both files in the same iteration. see the example and the script in my last edit
– MKJ
Nov 14 at 10:24
I don't see an_SA
in the examples, is this the problem.
– ctrl-alt-delor
Nov 14 at 10:27
Your example command takes two inputs and produces one output, but your list of example files doesn't have any "matches" between theB4
andB5
set. Please indicate which pairs of input files you want to use together, and how you decide the output filename each time.
– JigglyNaga
Nov 14 at 10:30
|
show 2 more comments
Please list an example input and output (just a few lines).
– ctrl-alt-delor
Nov 14 at 10:20
1
Why don't you loop over./*B[45]_SA.TIF
? Or do you need both files i the same iteration of the loop. Please explain more about what it is you're doing.
– Kusalananda
Nov 14 at 10:23
I need both files in the same iteration. see the example and the script in my last edit
– MKJ
Nov 14 at 10:24
I don't see an_SA
in the examples, is this the problem.
– ctrl-alt-delor
Nov 14 at 10:27
Your example command takes two inputs and produces one output, but your list of example files doesn't have any "matches" between theB4
andB5
set. Please indicate which pairs of input files you want to use together, and how you decide the output filename each time.
– JigglyNaga
Nov 14 at 10:30
Please list an example input and output (just a few lines).
– ctrl-alt-delor
Nov 14 at 10:20
Please list an example input and output (just a few lines).
– ctrl-alt-delor
Nov 14 at 10:20
1
1
Why don't you loop over
./*B[45]_SA.TIF
? Or do you need both files i the same iteration of the loop. Please explain more about what it is you're doing.– Kusalananda
Nov 14 at 10:23
Why don't you loop over
./*B[45]_SA.TIF
? Or do you need both files i the same iteration of the loop. Please explain more about what it is you're doing.– Kusalananda
Nov 14 at 10:23
I need both files in the same iteration. see the example and the script in my last edit
– MKJ
Nov 14 at 10:24
I need both files in the same iteration. see the example and the script in my last edit
– MKJ
Nov 14 at 10:24
I don't see an
_SA
in the examples, is this the problem.– ctrl-alt-delor
Nov 14 at 10:27
I don't see an
_SA
in the examples, is this the problem.– ctrl-alt-delor
Nov 14 at 10:27
Your example command takes two inputs and produces one output, but your list of example files doesn't have any "matches" between the
B4
and B5
set. Please indicate which pairs of input files you want to use together, and how you decide the output filename each time.– JigglyNaga
Nov 14 at 10:30
Your example command takes two inputs and produces one output, but your list of example files doesn't have any "matches" between the
B4
and B5
set. Please indicate which pairs of input files you want to use together, and how you decide the output filename each time.– JigglyNaga
Nov 14 at 10:30
|
show 2 more comments
3 Answers
3
active
oldest
votes
up vote
1
down vote
Loop over the files that constitute one half of the pairs, and for each such file, construct the filename of the corresponding pair and of the output file:
for b4_file in ./*_B4.TIF; do
b5_file="${b4_file%_B4.TIF}_B5.TIF"
ndvi_file="$HOME/Geodata/goteborg/croped/ndvi/${b4_file%_B4.TIF}_NDVI.TIF"
if [ ! -f "$b5_file" ]; then
printf 'Expected to find "%s" to go with "%s", but did notn' "$b5_file" "$b4_file" >&2
continue
fi
# do whatever it is you need to do with "$b4_file" and "$b5_file"
gdal_calc.py -A "$b4_file" -B "$b5_file" --type=Float32
--outfile="$ndvi_file --calc="(B-A)/(B+A)"
done
add a comment |
up vote
0
down vote
First note there is no-such thing as a file extension on Unix. .
is just another character. So here I search for some ½ of your files, strip the end of the name off, ready to put it back on (in two different ways).
This will sort of work, but may have some latent bugs.
for f in *_B5.TIF
do
i="$(echo "$f" | sed -r -e 's/(.*)_B5.TIF/1/')"
… ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF …
done
This should be more robust
find . -iname "*_B5.TIF" -print0 | sed -0r -e 's/(.*)_B5.TIF/1/' | xargs -0 -i … {}_B4_SA.TIF -B {}_B5_SA.TIF …
Please test, as I have not (may be some small error). And there is no error checking. Your program must check for miss-matches.
add a comment |
up vote
0
down vote
As you're using Bash, you can use Parameter expansion to chop the ending and save the common part of the filename in a new variable:
for f in *_B4.TIF ; do
i="${f%_B4.TIF}"
gdal_calc.py -A "${i}_B4.TIF" -B "${i}_B5.TIF" --outfile=".../${i}_NDVI.TIF"
done
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Loop over the files that constitute one half of the pairs, and for each such file, construct the filename of the corresponding pair and of the output file:
for b4_file in ./*_B4.TIF; do
b5_file="${b4_file%_B4.TIF}_B5.TIF"
ndvi_file="$HOME/Geodata/goteborg/croped/ndvi/${b4_file%_B4.TIF}_NDVI.TIF"
if [ ! -f "$b5_file" ]; then
printf 'Expected to find "%s" to go with "%s", but did notn' "$b5_file" "$b4_file" >&2
continue
fi
# do whatever it is you need to do with "$b4_file" and "$b5_file"
gdal_calc.py -A "$b4_file" -B "$b5_file" --type=Float32
--outfile="$ndvi_file --calc="(B-A)/(B+A)"
done
add a comment |
up vote
1
down vote
Loop over the files that constitute one half of the pairs, and for each such file, construct the filename of the corresponding pair and of the output file:
for b4_file in ./*_B4.TIF; do
b5_file="${b4_file%_B4.TIF}_B5.TIF"
ndvi_file="$HOME/Geodata/goteborg/croped/ndvi/${b4_file%_B4.TIF}_NDVI.TIF"
if [ ! -f "$b5_file" ]; then
printf 'Expected to find "%s" to go with "%s", but did notn' "$b5_file" "$b4_file" >&2
continue
fi
# do whatever it is you need to do with "$b4_file" and "$b5_file"
gdal_calc.py -A "$b4_file" -B "$b5_file" --type=Float32
--outfile="$ndvi_file --calc="(B-A)/(B+A)"
done
add a comment |
up vote
1
down vote
up vote
1
down vote
Loop over the files that constitute one half of the pairs, and for each such file, construct the filename of the corresponding pair and of the output file:
for b4_file in ./*_B4.TIF; do
b5_file="${b4_file%_B4.TIF}_B5.TIF"
ndvi_file="$HOME/Geodata/goteborg/croped/ndvi/${b4_file%_B4.TIF}_NDVI.TIF"
if [ ! -f "$b5_file" ]; then
printf 'Expected to find "%s" to go with "%s", but did notn' "$b5_file" "$b4_file" >&2
continue
fi
# do whatever it is you need to do with "$b4_file" and "$b5_file"
gdal_calc.py -A "$b4_file" -B "$b5_file" --type=Float32
--outfile="$ndvi_file --calc="(B-A)/(B+A)"
done
Loop over the files that constitute one half of the pairs, and for each such file, construct the filename of the corresponding pair and of the output file:
for b4_file in ./*_B4.TIF; do
b5_file="${b4_file%_B4.TIF}_B5.TIF"
ndvi_file="$HOME/Geodata/goteborg/croped/ndvi/${b4_file%_B4.TIF}_NDVI.TIF"
if [ ! -f "$b5_file" ]; then
printf 'Expected to find "%s" to go with "%s", but did notn' "$b5_file" "$b4_file" >&2
continue
fi
# do whatever it is you need to do with "$b4_file" and "$b5_file"
gdal_calc.py -A "$b4_file" -B "$b5_file" --type=Float32
--outfile="$ndvi_file --calc="(B-A)/(B+A)"
done
edited Nov 14 at 12:01
answered Nov 14 at 10:41
Kusalananda
115k15218351
115k15218351
add a comment |
add a comment |
up vote
0
down vote
First note there is no-such thing as a file extension on Unix. .
is just another character. So here I search for some ½ of your files, strip the end of the name off, ready to put it back on (in two different ways).
This will sort of work, but may have some latent bugs.
for f in *_B5.TIF
do
i="$(echo "$f" | sed -r -e 's/(.*)_B5.TIF/1/')"
… ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF …
done
This should be more robust
find . -iname "*_B5.TIF" -print0 | sed -0r -e 's/(.*)_B5.TIF/1/' | xargs -0 -i … {}_B4_SA.TIF -B {}_B5_SA.TIF …
Please test, as I have not (may be some small error). And there is no error checking. Your program must check for miss-matches.
add a comment |
up vote
0
down vote
First note there is no-such thing as a file extension on Unix. .
is just another character. So here I search for some ½ of your files, strip the end of the name off, ready to put it back on (in two different ways).
This will sort of work, but may have some latent bugs.
for f in *_B5.TIF
do
i="$(echo "$f" | sed -r -e 's/(.*)_B5.TIF/1/')"
… ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF …
done
This should be more robust
find . -iname "*_B5.TIF" -print0 | sed -0r -e 's/(.*)_B5.TIF/1/' | xargs -0 -i … {}_B4_SA.TIF -B {}_B5_SA.TIF …
Please test, as I have not (may be some small error). And there is no error checking. Your program must check for miss-matches.
add a comment |
up vote
0
down vote
up vote
0
down vote
First note there is no-such thing as a file extension on Unix. .
is just another character. So here I search for some ½ of your files, strip the end of the name off, ready to put it back on (in two different ways).
This will sort of work, but may have some latent bugs.
for f in *_B5.TIF
do
i="$(echo "$f" | sed -r -e 's/(.*)_B5.TIF/1/')"
… ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF …
done
This should be more robust
find . -iname "*_B5.TIF" -print0 | sed -0r -e 's/(.*)_B5.TIF/1/' | xargs -0 -i … {}_B4_SA.TIF -B {}_B5_SA.TIF …
Please test, as I have not (may be some small error). And there is no error checking. Your program must check for miss-matches.
First note there is no-such thing as a file extension on Unix. .
is just another character. So here I search for some ½ of your files, strip the end of the name off, ready to put it back on (in two different ways).
This will sort of work, but may have some latent bugs.
for f in *_B5.TIF
do
i="$(echo "$f" | sed -r -e 's/(.*)_B5.TIF/1/')"
… ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF …
done
This should be more robust
find . -iname "*_B5.TIF" -print0 | sed -0r -e 's/(.*)_B5.TIF/1/' | xargs -0 -i … {}_B4_SA.TIF -B {}_B5_SA.TIF …
Please test, as I have not (may be some small error). And there is no error checking. Your program must check for miss-matches.
answered Nov 14 at 10:39
ctrl-alt-delor
9,87031954
9,87031954
add a comment |
add a comment |
up vote
0
down vote
As you're using Bash, you can use Parameter expansion to chop the ending and save the common part of the filename in a new variable:
for f in *_B4.TIF ; do
i="${f%_B4.TIF}"
gdal_calc.py -A "${i}_B4.TIF" -B "${i}_B5.TIF" --outfile=".../${i}_NDVI.TIF"
done
add a comment |
up vote
0
down vote
As you're using Bash, you can use Parameter expansion to chop the ending and save the common part of the filename in a new variable:
for f in *_B4.TIF ; do
i="${f%_B4.TIF}"
gdal_calc.py -A "${i}_B4.TIF" -B "${i}_B5.TIF" --outfile=".../${i}_NDVI.TIF"
done
add a comment |
up vote
0
down vote
up vote
0
down vote
As you're using Bash, you can use Parameter expansion to chop the ending and save the common part of the filename in a new variable:
for f in *_B4.TIF ; do
i="${f%_B4.TIF}"
gdal_calc.py -A "${i}_B4.TIF" -B "${i}_B5.TIF" --outfile=".../${i}_NDVI.TIF"
done
As you're using Bash, you can use Parameter expansion to chop the ending and save the common part of the filename in a new variable:
for f in *_B4.TIF ; do
i="${f%_B4.TIF}"
gdal_calc.py -A "${i}_B4.TIF" -B "${i}_B5.TIF" --outfile=".../${i}_NDVI.TIF"
done
answered Nov 14 at 10:47
JigglyNaga
3,424728
3,424728
add a comment |
add a comment |
MKJ is a new contributor. Be nice, and check out our Code of Conduct.
MKJ is a new contributor. Be nice, and check out our Code of Conduct.
MKJ is a new contributor. Be nice, and check out our Code of Conduct.
MKJ is a new contributor. Be nice, and check out our Code of Conduct.
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%2f481667%2fhow-to-access-file-names-ending-before-the-extension-in-a-loop%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
Please list an example input and output (just a few lines).
– ctrl-alt-delor
Nov 14 at 10:20
1
Why don't you loop over
./*B[45]_SA.TIF
? Or do you need both files i the same iteration of the loop. Please explain more about what it is you're doing.– Kusalananda
Nov 14 at 10:23
I need both files in the same iteration. see the example and the script in my last edit
– MKJ
Nov 14 at 10:24
I don't see an
_SA
in the examples, is this the problem.– ctrl-alt-delor
Nov 14 at 10:27
Your example command takes two inputs and produces one output, but your list of example files doesn't have any "matches" between the
B4
andB5
set. Please indicate which pairs of input files you want to use together, and how you decide the output filename each time.– JigglyNaga
Nov 14 at 10:30