Each day I get 16 files in a path /Home/h87654/file/. The first line of every file will have a string containing that day’s date (YYYYMMDD). The position of date is not fixed, it can be anywhere on the first line.i want to get the latest file arrived each day based on the date on first line .

apologies for question being too broad.

am new to unix . below is the code i am working on .

/Home/h87654/latest_file.txt this will have all the 16 files having respective date in first line. out of those 16 , i need to get the latest file with timestamp for ex: if we are running the script today ,ie 2018-06-11 . start date would be 20180501 and end_date would be 20180531 in first loop , latest_file.txt will have all files containing 20180501 in first line. i need the file which came the last and its timestamp and write both to time.txt

secod loop , latest_file.txt will have all files containing 20180502 in first line,i need the file which came the last and its timestamp and append both to time.txt

thrid loop,latest_file.txt will have all files containing 20180503 in first line,i need the file which came the last and its timestamp and appendboth to time.txtand so on till the month end . $end_date

cd /Home/h87654/file/
start_date=`date -d "-1 month -$(($(date +%d)-1)) days" +%Y%m%d` ##start of month in  yyyymmdd
end_date=`date -d "-$(date +%d) days " +%Y%m%d` ##end of month in  yyyymmdd

while [ "$start_date" > "$end_date" ]; ##execute for each day for a  monnth

awk 'FNR==1{if($0~"$start_date")print FILENAME;}'  /Home/h87654/file/*.* >/Home/h87654/latest_file.txt   ##check ist date in the first line and print the file name 
##am stuck here .. idk how to proceed 


closed as too broad by Rui F Ribeiro, Jesse_b, Romeo Ninov, glenn jackman, telcoM Jun 10 '18 at 2:02

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    This is not a order-my-script forum. Please detail what you have done until now, your questions, and how we might be able to help you. – Rui F Ribeiro Jun 9 '18 at 11:50
  • 1
    This is unclear. You get 16 files per day? Do all files have today's date in them? If so how do you determine the last file? What determines the date offsets of your "loops"? You mention it should start with 20180214 and then move onto 20180202, should it check for every 12 days or is there some other factor determining that? – Jesse_b Jun 9 '18 at 13:19
  • Hi Jesse_b , apologies for the insufficient content. now to your question , 1.yes i get 16 files per day .few files might get late and receive it next day too. 2.yes .all 16 files will have same date .yyyymmdd 3. i need to deteremine the last file based on the timstamp. 4. i need to run the loop everyday for one month . – iwillbegr8.1day Jun 11 '18 at 11:14
  • Advice: read this, it can significantly popularize your posts. – peterh Jun 11 '18 at 11:59

Not sure what you're asking exactly.

To get the path of a file with the greatest first sequence of 8 digits in their first line, with zsh:

getdate() {
  local MATCH
  IFS= read -r < $REPLY && [[ $REPLY =~ '[[:digit:]]{8}' ]]

printf '%s\n' /Home/h87654/file/*.*(O+getdate[1])

To get the greatest date and all the files that contain that greatest date as the first sequence of 8 digits in their first line, you could do something like:

typeset -A bydate
for file (/Home/h87654/file/*.*) {
  IFS= read -r line < $file &&
    [[ $line =~ '[[:digit:]]{8]' ]] &&
printf 'Latest date: %s\nFiles:\n%s' $latest $bydate[$latest]

Not the answer you're looking for? Browse other questions tagged or ask your own question.