LinuxQuestions.org
Visit the LQ Articles and Editorials section
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

Closed Thread
 
Search this Thread
Old 12-28-2004, 09:22 AM   #1
arnulfo
LQ Newbie
 
Registered: Dec 2004
Posts: 7

Rep: Reputation: 0
sorting and stuff


first, how do you sort lines of a file given that each line has a date of format
%d %b %Y

second, how do you display a list of numbers in xxxx.xx and add their sum?

thanks, peace
 
Old 12-28-2004, 10:47 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Re: sorting and stuff

Quote:
Originally posted by arnulfo
first, how do you sort lines of a file given that each line has a date of format
%d %b %Y
You could have been a little more specific, i.e. what programming language, and how do the lines look like, especially after the date. Assuming bash, and that there will be space after the date, and that the dates are at the start of the line this bash-script will do it:
Code:
#!/bin/bash

FILE=datum.txt

grep -v '^ *$' $FILE | while read LINE ; do
	DATUM=$(date -d "$(echo $LINE | cut -d' ' -f1,2,3)" +%s)
	echo $DATUM:$LINE
done | sort -n | cut -d: -f2-
If there are only dates on the line in the file (say "datum.txt"), then do this:


Last edited by Hko; 12-28-2004 at 10:53 AM.
 
Old 12-28-2004, 11:29 AM   #3
arnulfo
LQ Newbie
 
Registered: Dec 2004
Posts: 7

Original Poster
Rep: Reputation: 0
Yes, its in bash.
No, the file does not only contain dates

its in this format:
"some name|some number|`date "+%b %d %Y"`"
"another name|another number|another date of same format"
.
.
.
.
etc
 
Old 12-28-2004, 03:09 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Can you copy and paste a sample of the file? Are you wanting to sort by date? Also paste some of the numbers which you want to calculate.
 
Old 12-28-2004, 06:50 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by arnulfo
Yes, its in bash.
No, the file does not only contain dates

its in this format:
"some name|some number|`date "+%b %d %Y"`"
"another name|another number|another date of same format"
Then try this:
Code:
#!/bin/bash

FILE=datum.txt
sed -e '/^ *$/d' -e 's/.*|\([^"]*\).*/\1/' $FILE | while read LINE ; do
	DATUM=$(date -d "$(echo $LINE | cut -d' ' -f1,2,3)" +%s)
	echo $DATUM:$LINE
done | sort -n | cut -d: -f2-
 
Old 12-29-2004, 03:06 AM   #6
arnulfo
LQ Newbie
 
Registered: Dec 2004
Posts: 7

Original Poster
Rep: Reputation: 0
yep got it. thanks.
say the text file is:

Book|120|23 Dec 2004
Tea|20.3|24 Dec 2003
watch|1015.50|1 Jan 2005
coffee|10.00|7 Nov 2001

How do you display the prices in this manner:

Book 20.00 23 Dec 2004
Tea 20.30 24 Dec 2003
watch 1015.50 1 Jan 2005
coffee 10.00 7 Nov 2001

prices must be right-justified so that each digit are aligned to each other down to the last cent and output is another text file

Last edited by arnulfo; 12-29-2004 at 03:19 AM.
 
Old 12-29-2004, 03:44 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
It would be easier to change the date format to
%Y%m%d
20041212
then it can easily be sorted.
Is this possible, do you have control?

for doing all these operations including the sum probably best use perl.

billy
 
Old 12-29-2004, 03:50 AM   #8
arnulfo
LQ Newbie
 
Registered: Dec 2004
Posts: 7

Original Poster
Rep: Reputation: 0
ive already done the sort by date part.
what i need is to format the prices from the raw data into the output i said earlier.
and i cant use perl because this is a homework hehe
 
Old 12-29-2004, 04:02 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
well, with Korn shell you can typeset -R -L
a variable, which justifies it when output.

you do like,

typeset -R10 var_name (e.g. 10 chars width)
before using it.
dunno if bash has it.

billy
 
Old 12-29-2004, 04:05 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
e.g:

Code:
billym.primadtpdev>typeset -R10 date
billym.primadtpdev>date=123.45
billym.primadtpdev>echo "|$date|"   
|    123.45|
 
Old 12-29-2004, 05:57 AM   #11
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by arnulfo
ive already done the sort by date part.
No. I did.

Quote:
and i cant use perl because this is a homework hehe
You shouldn't ask people here to do your homework.
Why?
  • You don't learn anything. (or at least very little)
  • It's unfair
  • It's against the rules of this forum
  • You abuse people's willingness to help you (like me in this case).
If you did things yourself, and then get stuck, post the code you have so far, and state that it is a home work assignment... That would be different, and OK.
 
Old 01-01-2005, 07:18 AM   #12
arnulfo
LQ Newbie
 
Registered: Dec 2004
Posts: 7

Original Poster
Rep: Reputation: 0
who are you to judge someone that he diditn learn anything. who cares if its a homework. im not asking you and just copying every code that yo write here. and by the way, its not a homework, its a project. and its just a boogerly size of a problem compared to the whole code. I just didnt know that there was a %s
option for date, thats all. so dont be too self-righteous. "abuse my willingness"... hahahaha
 
Old 01-01-2005, 10:33 AM   #13
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Quote:
who are you to judge someone that he diditn learn anything. who cares if its a homework.
Well it is a rule at this forum and if you ever want to get help again, you need to improve your responses to bash wizards like Hko.
 
Old 01-01-2005, 04:37 PM   #14
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Firing off on those who spend their free time helping you is not the best idea. Especially at (as homey put it "bash wizards") like Hko.

I usually allow homework questions in my forum as long I see some effort on the posters behalf.

Quote:
How do you display the prices in this manner:

Book 20.00 23 Dec 2004
Tea 20.30 24 Dec 2003
watch 1015.50 1 Jan 2005
coffee 10.00 7 Nov 2001

prices must be right-justified so that each digit are aligned to each other down to the last cent and output is another text file
Where is your attempt at the problem?

Next time try:
A) not insulting our members that try to help you
B) post a question like: what's wrong with this code... and it may stay open longer.

closing thread
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sorting data. unreal128 Linux - Software 1 09-20-2005 06:31 PM
Which sorting algorithm? nodger Programming 6 01-28-2005 06:13 PM
When installing new stuff in suse 9.1, do you uninstall old stuff first? randon Suse/Novell 1 12-25-2004 04:37 PM
Sorting Beppe83 Linux - Software 7 06-21-2004 09:10 AM
Sorting question AMMullan Linux - General 12 01-19-2004 03:09 PM


All times are GMT -5. The time now is 05:38 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration