2

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

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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"

5 Comments

thank you very much.this solution works; i am very happy about that. I am greedy ;). now I am little bit more ambitious.. what if, I want to add ... "$f3 = 001 to 007". what would be the correct syntax for that?? thank you in advance....
@neel: Use -gt and -lt to compare numbers, like:[ "$f3" -gt "000" ] && [ "$f3" -lt "008" ]
Grate, Awesome....Thank you very mush for the effort; i really appreciate it but u know, nothing is enough for me. :). ######### ok, now the next help I need is: in my "$f2" column I have data ranged from "2006" to "2013" (year) and in "$f3" column, from "000" to "365" (day of year). I need to find a efficient looping method which will echo "$f1" based on year wise weekly condition in different text file name like 2006_01.txt, 2006_52, 2007_03.txt, 2007_52.txt and so...... any help, I might get?
... i was able to automate part of my need but still i have to find out how i can deal the weeks.. I Have put my code in the comment section, bellow
@neel: That can be considered a different question. You should better create a new one for it.
1

You are echo-ing $f1 always, regardless of whether the line matched. Try this instead:

[[ "${f2}" == "2013" ]] && [[ "${f3}" == "011" ]] && echo "${f1}"

Comments

0

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.

Comments

0

... 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"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.