LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sorting and stuff (https://www.linuxquestions.org/questions/programming-9/sorting-and-stuff-271132/)

arnulfo 12-28-2004 09:22 AM

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

Hko 12-28-2004 10:47 AM

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:


arnulfo 12-28-2004 11:29 AM

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

homey 12-28-2004 03:09 PM

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.

Hko 12-28-2004 06:50 PM

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-


arnulfo 12-29-2004 03:06 AM

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

bigearsbilly 12-29-2004 03:44 AM

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

arnulfo 12-29-2004 03:50 AM

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

bigearsbilly 12-29-2004 04:02 AM

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

bigearsbilly 12-29-2004 04:05 AM

e.g:

Code:


billym.primadtpdev>typeset -R10 date
billym.primadtpdev>date=123.45
billym.primadtpdev>echo "|$date|" 
|    123.45|


Hko 12-29-2004 05:57 AM

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.

arnulfo 01-01-2005 07:18 AM

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

homey 01-01-2005 10:33 AM

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.

crabboy 01-01-2005 04:37 PM

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


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