extracting columns falling within specific ranges for multiple files

Hi, I need to create weekly files from daily records stored in individual monthly filenames from 1999-2010. my sample file structure is like the ones below:

daily record stored per month: 
199901.xyz, 199902.xyz, 199903.xyz, 199904.xyz ...199912.xyz

records inside 199901.xyz (original data has no header)
lat lon wind year month day
12.5 121.0 3.4 1999 1 1
12.4 121.1 2.2 1999 1 1
12.1 122.1 1.2 1999 1 2
12.3 124.3 1.0 1999 1 3 
12.5 122.2 1.8 1999 1 3...
12.1 123.7 2.3 1999 1 31

I want to output the values of lat,lon,wind based on a 7-day range and end up with this below:

199901_wk01.txt contains the values of lat,lon,wind for days from 1-7
199901_wk02.txt contains the values of lat,lon,wind for days 8-14
199901_wk03.txt contains the values of lat,lon,wind for days 15-21
199901_wk04.txt contains the values of lat,lon,wind for days 22-28

199902_wk01.txt contains the values of lat,lon,wind for days from 1-7
199902_wk02.txt contains the values of lat,lon,wind for days from 8-14
199902_wk03.txt contains the values of lat,lon,wind for days from 15-21
199902_wk04.txt contains the values of lat,lon,wind for days from 22-28

and so on for the rest of the files.

Thanks much in advance.

awk 'BEGIN { MIN=-6 }; FNR==1 { MIN+=7 }; ($6 >= MIN) && ($6 <= (MIN+7))' file1 file2 file3 ...

Every time it switches files, it'll bump the value of MIN by 7.

If that's not the output you wanted, you'll have to be more specific -- show the exact output you want and how you calculated it, not just the things you want in it...

Hi, thanks for your reply. Am sorry for the unclear requirement I have earlier posted. The exact output I wanted would contain these values. The daily records for each month vary and so I need to print all the column values depending on whether the day column is within a specific weekly bin.

199901_wk01.txt contains
12.5 121.0 3.4 1999 1 1 
12.4 121.1 2.2 1999 1 1 
12.1 122.1 1.2 1999 1 2 
12.3 124.3 1.0 1999 1 3  
12.5 122.2 1.8 1999 1 3
12.5 122.5 1.6 1999 1 5
12.3 121.4 1.2 1999 1 7
#! /bin/bash

for file in *.txt
do
    f=`basename $file .txt`
    while read x
    do
        day=`echo $x | awk '{print $NF}'`
        if [ $day -ge 1 ] && [ $day -le 7 ]
        then
            echo $x >> ${f}_wk01.txt
        elif [ $day -ge 8 ] && [ $day -le 14 ]
        then
            echo $x >> ${f}_wk02.txt
        elif [ $day -ge 15 ] && [ $day -le 21 ]
        then
            echo $x >> ${f}_wk03.txt
        elif [ $day -ge 22 ] && [ $day -le 28 ]
        then
            echo $x >> ${f}_wk04.txt
        elif [ $day -ge 29 ] && [ $day -le 31 ]
        then
            echo $x >> ${f}_wk05.txt
        fi
    done < $file
done
1 Like

thanks much balajesuri. i have a question on the code, the files has no header, which part on the code scans the specific column corresponding to days?

oh ok. am sorry, its ok now. thanks much as always for being helpful.:slight_smile: