Reputation: 25
i have a text file contains following information:
/media/Arc/eo_archive_test/MODIS/L2_LAC_OC/A2006001071000.L2_LAC_OC.x.hdf:2006:365
/media/Arc/eo_archive_test/MODIS/L2_LAC_OC/A2006001084000.L2_LAC_OC.x.hdf:2013:011
/media/Arc/eo_archive_test/MODIS/L2_LAC_OC/A2006001084500.L2_LAC_OC.x.hdf:2005:365
....
I want to echo $f1 where $f2 is "2013" and $f3 is "011". i am using following script but its echoing all. what am i doing wrong here?
file=/media/Arc/eo_archive_test/MODIS/input/all_file_list.txt
while IFS=: read -r f1 f2 f3
do
[ "$f2" = "2013" ] || [ "$f3" = "011" ]
echo $f1
done < "$file"
any solution? Thank you in advance
Upvotes: 2
Views: 189
Reputation: 25
... as mentioned above, i was able to automate part of my need; a little more help required, here is the code....
file=/media/Arc/eo_archive_test/MODIS/input/all_file_list.txt
while IFS=: read -r f1 f2 f3
do
for year in "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013"
do
[[ $f2 == $year ]]
for doy in "001" "002" "003" "004" "005" "006" "007" # here we need some automation; seeking help
do
[[ $f3 == $doy ]] && echo $f1 >> test.txt
done
done
done < "$file"
Upvotes: 0
Reputation: 189307
This fits Awk's processing model hand in glove.
awk -F : '$2 == "2013" && $3 == "011" { print $1 }' /media/Arc/eo_archive_test/MODIS/input/all_file_list.txt
If you want a regex instead, that's done like $3 ~ /^00[1-7]$/
or you could do a numeric comparison $3 > 0 && $3 <= 7
.
Upvotes: 0
Reputation: 62369
You are echo
-ing $f1
always, regardless of whether the line matched. Try this instead:
[[ "${f2}" == "2013" ]] && [[ "${f3}" == "011" ]] && echo "${f1}"
Upvotes: 1
Reputation: 246744
You need to connect the test to the action
[[ $f2 == 2013 && $f3 == 011 ]] && echo $f1
or
if [ "$f2" = "2013" ] && [ "$f3" = "011" ]; then
echo $f1
fi
What you are doing now is performing the test commands and ignoring the exit status. So the echo command does indeed execute for every line.
Upvotes: 2
Reputation: 36252
You have to include the comparison inside an if
sentence and use &&
instead of ||
, like:
file=/media/Arc/eo_archive_test/MODIS/input/all_file_list.txt
while IFS=: read -r f1 f2 f3
do
if [ "$f2" = "2013" ] && [ "$f3" = "011" ]; then
echo $f1
fi
done < "$file"
Upvotes: 1