Home > other >  how can i select a file under directories in a loop and print data from it?
how can i select a file under directories in a loop and print data from it?

Time:11-15

I need to find a file with a .gz extension among files in a loop and extract some data from it and print it.

i have folders like d091,d092,.....,d150 and under these folders there are different files with .gz extension. I need to print some data from these .gz files. the location of the data in the file as I specified.

this is the code i try to use but it didn't work. how can i specify the path in for loop?

shopt -s nullglob
shopt -s failglob

for k in {091..099}; do
for file in $(ls *.gz)
    do
       echo ${file:0:4} | tee -a receiver_ids 
       echo ${file:16:17} | tee -a doy 
       echo ${file:0:100} | tee -a data_record 
done
done

CodePudding user response:

I am unsure about your echo | tee part correctness, but for the rest you could try something like:

shopt -s nullglob

for dir in d[0-9][0-9][0-9]; do
    printf "processing %s\n" "$dir"
    if [[ -d "$dir" ]]; then
        cd "$dir"                                 # enter the directory
        for file in *.gz; do                      # never use ls to get a files list
            printf "found [%s] file.\n" "$file"
        done
        cd ..                                     # come back to parent dir
    fi
done

If you don't need to separate directories, it could be shortened to a single loop:

for file in d[0-9][0-9][0-9]/*.gz; do
    fn="${file##*/}"
    dir="${file%%/*}"
    printf "found [%s] file in [%s] dir\n" "$fn" "$dir"
done

CodePudding user response:

I guess you are allowed to use find?

for file in $(find . -name "*.gz"); do 
  file=$(basename ${file})
  echo ${file:0:4} | tee -a receiver_ids
  echo ${file:16:17} | tee -a doy
  echo ${file:0:100} | tee -a data_record 
done
  • Related