Unix Programming - Days to Month file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2005 > Days to Month file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Days to Month file
prasad413in@gmail.com

2005-11-02, 7:53 am

I am a newbie to UNIX
I have a Text file one a single days data, the text file is like
200510312245.txt
like YYYYMMDDHHMM.txt, where YYYY indicates Year, MM indicates Month
and so on.. I have all the Days file in a Year, Now I need to combine
all the Days in a Month to a single Month file like 200510.txt
There are a few lines of header in each file which should remain same
in Month File. How is it possible to combine in Real- time.

Thanks
Prasad

Bit Twister

2005-11-02, 7:53 am

On 2 Nov 2005 02:20:42 -0800, prasad413in@gmail.com wrote:
> I am a newbie to UNIX


Then the following should help but not all commands may work in your shell.

! bash script introduction documentation
http://tldp.org/LDP/intro-linux/html/index.html

! bash script advanced documentation
http://tldp.org/LDP/abs/html/index.html

For extra points, you may want to do a
man test

> I have a Text file one a single days data, the text file is like
> 200510312245.txt
> like YYYYMMDDHHMM.txt, where YYYY indicates Year, MM indicates Month
> and so on.. I have all the Days file in a Year, Now I need to combine
> all the Days in a Month to a single Month file like 200510.txt
> There are a few lines of header in each file which should remain same
> in Month File. How is it possible to combine in Real- time.


I would guess you could use something like this untested snippet.

ym=$(date +%Y%m)
ymd=$(date +%Y%m%d)

mo_fn=${ymd}.txt

if [ ! -e $mo_fn ] ; then
echo "line 1 header " > $ymd.txt
echo "line 2 header " >> $ymd.txt
fi

for day_fn in ${ymd}????.txt ; do
cat $day_fn >> $mo_fn
done

For your "combine in Real- time" requirement, changing the wild card
???? to HHMM is left as an exercise for the student.

I do suggest creating a directory for testing
with a 24 hour test set of files and at least 2 files of YYYYMMDD*txt

Chris F.A. Johnson

2005-11-02, 5:56 pm

On 2005-11-02, Bit Twister wrote:
> On 2 Nov 2005 02:20:42 -0800, prasad413in@gmail.com wrote:
>
> Then the following should help but not all commands may work in your shell.
>
> ! bash script introduction documentation
> http://tldp.org/LDP/intro-linux/html/index.html
>
> ! bash script advanced documentation
> http://tldp.org/LDP/abs/html/index.html
>
> For extra points, you may want to do a
> man test
>
>
> I would guess you could use something like this untested snippet.
>
> ym=$(date +%Y%m)
> ymd=$(date +%Y%m%d)


What if the month changes between the first and second call to
date?

You only need, and should only use, one date command:

eval "$(date ym=+%Y%, ymd=+%Y%m%d)"

Or:

ymd=$(date +%Y%m%d)
ym=${ymd%??}

> mo_fn=${ymd}.txt
>
> if [ ! -e $mo_fn ] ; then
> echo "line 1 header " > $ymd.txt
> echo "line 2 header " >> $ymd.txt
> fi


[ -e "$mo_fn" ] || printf "%s\n" "line 1 header " "line 2 header " > $mo_fn"

> for day_fn in ${ymd}????.txt ; do
> cat $day_fn >> $mo_fn
> done


cat ${ymd}????.txt >> "$mo_fn"

> For your "combine in Real- time" requirement, changing the wild card
> ???? to HHMM is left as an exercise for the student.
>
> I do suggest creating a directory for testing
> with a 24 hour test set of files and at least 2 files of YYYYMMDD*txt
>



--
Chris F.A. Johnson | Author:
<http://cfaj.freeshell.org> | Shell Scripting Recipes:
Any code in this post is released | A Problem-Solution Approach,
under the GNU General Public Licence | 2005, Apress
Bit Twister

2005-11-02, 5:56 pm

On Wed, 2 Nov 2005 12:46:19 -0500, Chris F.A. Johnson wrote:
> On 2005-11-02, Bit Twister wrote:
>
> What if the month changes between the first and second call to
> date?


How is the teacher going to supprise the student when you give out all
the answers? :-)

The OP trying to snag *HHMM files, has the same kind of problem.

> You only need, and should only use, one date command:
>
> eval "$(date ym=+%Y%, ymd=+%Y%m%d)"


date: too many non-option arguments: ymd=+%Y%m%d

> Or:
>
> ymd=$(date +%Y%m%d)
> ym=${ymd%??}


Which also helps when you want to regression test the code. You can modify
ymd before it is used by other variables.




> [ -e "$mo_fn" ] || printf "%s\n" "line 1 header " "line 2 header " > $mo_fn"


or with ! -e and without the trailing "
[ ! -e "$mo_fn" ] || printf "%s\n" "line 1 header " "line 2 header " > $mo_fn

All though elegant, gets a better Quality Assurance score, and is faster,
as a maintenance programmer, it sucks when trying
to see what goes on, at a glance, when using
set -xv

Over all, I think the task makes a pretty good homework exercise.

Jordan Abel

2005-11-02, 5:56 pm

On 2005-11-02, Bit Twister <BitTwister@mouse-potato.com> wrote:
>
> date: too many non-option arguments: ymd=+%Y%m%d


I assume he meant to say: eval $(date +ym=%Y%m,\ ymd=%Y%m%d)
Bit Twister

2005-11-02, 5:56 pm

On Wed, 2 Nov 2005 18:54:12 +0000 (UTC), Jordan Abel wrote:
> On 2005-11-02, Bit Twister <BitTwister@mouse-potato.com> wrote:
>
> I assume he meant to say: eval $(date +ym=%Y%m,\ ymd=%Y%m%d)


Did you do an
echo $ym $ymd


Jordan Abel

2005-11-02, 5:56 pm

On 2005-11-02, Bit Twister <BitTwister@mouse-potato.com> wrote:
> On Wed, 2 Nov 2005 18:54:12 +0000 (UTC), Jordan Abel wrote:
>
> Did you do an
> echo $ym $ymd


oops - no comma - i was confused by the one in the original example, which was
apparently a typo for 'm'

eval $(date +ym=%Y%m\ ymd=%Y%m%d)
Chris F.A. Johnson

2005-11-02, 5:56 pm

On 2005-11-02, Jordan Abel wrote:
> On 2005-11-02, Bit Twister <BitTwister@mouse-potato.com> wrote:
>
> I assume he meant to say: eval $(date +ym=%Y%m,\ ymd=%Y%m%d)


Almost.

eval "$(date "+ym=%Y%m ymd=%Y%m%d")"

I shouldn't post before my first cup of coffee.

--
Chris F.A. Johnson | Author:
<http://cfaj.freeshell.org> | Shell Scripting Recipes:
Any code in this post is released | A Problem-Solution Approach,
under the GNU General Public Licence | 2005, Apress
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com