LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2008, 10:44 AM   #1
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Rep: Reputation: 0
concatenate files sorted by date


I am a beginner in script writing, i tried to do the following
I have a set of files sorted by date in the format YYMMDD.s and .x and .r
I need to concatenate a header file to these sets of files so I used the following code
Code:
echo "enter Swath number"
read s
echo "please enter first date and MMDD press ENTER"
read i
echo "please enter last date and MMDD  press ENTER"
read j
for((i; i <= j; i++))
do
cat NJSC_Alam_Ph1_Blk1_3D_HDR_S /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}.s  > /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}_Final.s

cat NJSC_Alam_Ph1_Blk1_3D_HDR_R /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}.r  > /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}_Final.r

cat NJSC_Alam_Ph1_Blk1_3D_HDR_X /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}.x  > /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}_Final.x

done
I expect it to concatenate the files based on the user input.
I have 2 problems, the first on is that it doesn't do this for example if I enter the value for i and j to be 0530 and 0531 for 30 and 31 of May it searches for a file named 08345 instead of 080530.
the other problem is how can i make the script to work for 2 different months? like I have the first file as 300508 and last file as 050608, the loop is not going to work there
 
Old 06-07-2008, 12:05 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Wouldn't it be easier to increment using "/bin/date '+%Y%m%d' --date "+${n} days"" from the starting date? At least you'll end up with valid YYYYMMDD values.
 
Old 06-07-2008, 03:04 PM   #3
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
can you please explain more how to do that? as I said I am a beginner so i still don't know most of the command syntax
 
Old 06-07-2008, 09:39 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
It is late and I am too lazy for debugging your script, but...

First, it is likeley that Bash trims the first zero off your input once it recognizes a valid number like 0530 -> 530. Dunno how 0530 ends up to 345, but I hope that is a typo.

Secondly, it would be better to have the number you entered reformatted with the date command. (man date). In that case ANY valid date or part of it (MMDD) will be converted to a very strictly defined format by date command, including leading zeroes. You do this by specifying a date string to the date command and have the date command give you an output according to your format specified.

Last, run your script with
Code:
sh -x yourscript
It shows you every command being executed that the value of each variable. A rude way of debugging, but debugging consist for 90% of looking where something goes wrong. Once you see that you are smart enough to solve it.

jlinkels
 
Old 08-16-2008, 06:14 PM   #5
docaia
LQ Newbie
 
Registered: Jan 2008
Posts: 18

Original Poster
Rep: Reputation: 0
what is the script command to identify a date variable? is there a variable called date?
I tried something like date i, and dim i as date but it didn't work.

I also tried "date -d ${i} +"%y%m%d"" but it said that i is not a command

can you please help me on that?
 
Old 08-16-2008, 08:32 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
date is a command. Run it from the command line and see if it exists. If not, install it.

Next, try my suggestion sh -x and see what ${i} expands to. If $i for example is "050708", run the command on the command line and see what it does. Take care about quoting, that is likely to go wrong

Debugging is looking at what your program does differently as expected, and if you don't understand experiment step by step until you find out why.

Also, pay attention to what UnSpawn said about generating valid dates. Your script won't do that, as after 0531 you will calculate 0532, 0533, while you want 0601, 0602. UnSpawn's suggestion is to let 'date' do the calculation by adding an offset in days in the -d string.

Take a look at this modified script. I went thru seconds instead of days.

Code:
#! /bin/bash

echo "enter Swath number"
read s
echo "please enter first date and MMDD press ENTER"
read i
echo "please enter last date and MMDD  press ENTER"
read j

i_ts=$(date -d 08${i} +%s)
j_ts=$(date -d 08${j} +%s)

for((i_ts=$i_ts; i_ts <= j_ts; i_ts=(($i_ts+86400)) ))
do

        ds="1970-01-01 $i_ts sec"
        date=$(date -d  "$ds"  +%y%m%d )
        echo "i_ts: $i_ts, j_ts: $j_ts, i: $date"

done
The output is this, and comes close to what you need.

Code:
donald_pc:/tmp$ ./t.sh
donald_pc:/tmp$ ./t.sh
enter Swath number
44
please enter first date and MMDD press ENTER
0529
please enter last date and MMDD  press ENTER
0602
i_ts: 1212033600, j_ts: 1212379200, fdate: 080529
i_ts: 1212120000, j_ts: 1212379200, fdate: 080530
i_ts: 1212206400, j_ts: 1212379200, fdate: 080531
i_ts: 1212292800, j_ts: 1212379200, fdate: 080601
i_ts: 1212379200, j_ts: 1212379200, fdate: 080602
donald_pc:/tmp$
jlinkels
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate mpeg (vob) files, command line tool. kaz2100 Linux - Software 2 02-09-2008 05:42 PM
Script to concatenate several files docaia Linux - General 10 02-03-2008 01:59 PM
Concatenate PDF files? mykrob Linux - Software 5 11-07-2006 05:25 AM
Modify the way files are alphabetically sorted Sabinou Linux - General 8 08-11-2006 02:44 AM
concatenate binary files???? justin19fl Linux - Newbie 6 05-14-2001 02:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration