You are not logged in.

#1 2012-09-09 09:59:56

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

[Solved] Still learning and stuck with 'date'

Hi there, I thought it was about time I learned the fine art of shell scripting, but as the title states, I'm stuck.

A file with some text, one of which is this:

Download time: September 8 12:17:37

This I need to grap and convert to a computer readable format, so that I may compare it with another.
This shouldn't be too hard:

cat ~/unitinfo.txt | grep 'Download time: ' | cut -c16- | xargs date +"%B %-d %T" -d

But, when I execute this above, I get the error:

date: extra operand '8'

I've tried various variations, but nothing seems to make a difference, all thought the format is a perfect match when I get 'date' to print it.


Please advice and best regards.

Last edited by zacariaz (2012-09-09 10:17:14)


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#2 2012-09-09 10:09:33

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] Still learning and stuck with 'date'

If you wish to use xargs, it must look like this:

echo september 8 | xargs -i date -d "{}"

Otherwise you can do it like this:

date -d "$(echo september 8)"

edit:
You can also use

echo september 8 | xargs --null date -d

to group all words together in one argument.

Last edited by Procyon (2012-09-09 10:13:11)

Offline

#3 2012-09-09 10:12:11

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Still learning and stuck with 'date'

ha! that works, thanks a bunch!


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#4 2012-09-09 10:16:01

Awebb
Member
Registered: 2010-05-06
Posts: 6,688

Re: [Solved] Still learning and stuck with 'date'

Try removing complexity to indentify the problem:

$ date +"%B %-d %Tq" -d September 8 12:17:37date: extra operand ‘8’
Try 'date --help' for more information.

However, this happens very often:

$ date +"%B %-d %Tq" -d "September 8 12:17:37"
September 8 12:17:37q

What is this q about?

The correct xargs syntax here would be:

$ xargs -I {} date +"%B %-d %Tq" -d "{}"

I'd normally not hold your hand like this, but this one took me hours of reasearch back then, that could have been spent more worthwhile.

However, don't use xargs if you don't need to. It's ugly in a "don't waste those pipes" way.

date +"%B$ %-d %T" -d "$(echo "Download time: September 8 12:17:37" | cut -c16-)"

By the way: Google for "useless use of cat" :-D

Offline

#5 2012-09-09 10:23:30

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Still learning and stuck with 'date'

1. The q was a typo that has not been involved in testing. However, it would have coursed me trouble later on, so thank you for catching that.
2. I'll take your advice regarding xargs. I really don't care about the method as long as it works.
3. As for useless use of cat, you're of course right. Guess it's what happens when you take things one step at the time.

Right now I'm wondering how exactly 'date' is going to help me, as it just outputs the same string as I input. I can't figure out how to specify the output and I need to get a number I can do some math on.


In any event, thanks again


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#6 2012-09-09 10:32:19

Awebb
Member
Registered: 2010-05-06
Posts: 6,688

Re: [Solved] Still learning and stuck with 'date'

Time for regular expression and bash variables, I'd say.

Offline

#7 2012-09-09 10:34:31

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Still learning and stuck with 'date'

Awebb wrote:

Time for regular expression and bash variables, I'd say.

I can see what variable has to do with it and I am of curse using those oddities already, but I don't get what you are getting at with the regular expressions. I just need date to output the number of second since what's the date, 1970 something.

It would seem that 'date' am perfectly capable og figuring out the format on it's own. Can that really be?

Last edited by zacariaz (2012-09-09 10:40:01)


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#8 2012-09-09 10:53:21

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Still learning and stuck with 'date'

Just for the fun of it, if you care to comment on my progress so far:

unitinfo.txt:

Current Work Unit
-----------------
Name: Gromacs
Tag: -
Download time: September 8 12:17:37
Due time: September 12 12:17:37
Progress: 25%  [||________]

fah.sh:

#!/bin/bash
dueTime="$(date +%s -d "$(grep 'Due time: ' ~/unitinfo.txt | cut -c11-)")"
downloadTime="$(date +%s -d "$(grep 'Download time: ' ~/unitinfo.txt | cut -c16-)")"
currentTime="$(date +%s)"
progress="$(grep 'Progress: ' ~/unitinfo.txt | cut -c11-c12 | sed 's/ *$//')"

Now that I look at it again, am I correct that

sed 's/ *$//'

might as well be

sed 's/ $//'

?

In any case, there a long way to go. wink

Last edited by zacariaz (2012-09-09 22:20:45)


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#9 2012-09-09 22:19:27

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [Solved] Still learning and stuck with 'date'

If you want to be clever, try this.

#!/bin/bash

declare -A data

while IFS=: read key value; do
	[[ -n "$value" ]] || continue
	value=$(echo $value)
#	printf "KEY <%s>\tVALUE <%s>\n" "$key" "$value"
	case "$key" in
		D*) data[$key]=$(date +%s -d "$value") ;;
		P*) data[$key]=${value%%\%*} ;;
		* ) data[$key]=$value
	esac
done < ~/unitinfo.txt

for d in "${!data[@]}"; do
	echo "$d: ${data[$d]}"
done

Offline

#10 2012-09-09 23:07:27

Awebb
Member
Registered: 2010-05-06
Posts: 6,688

Re: [Solved] Still learning and stuck with 'date'

zacariaz wrote:

Now that I look at it again, am I correct that

sed 's/ *$//'

might as well be

sed 's/ $//'

?

"Might as well be" as in "there are cases in which it is" and not "it always is a synonym".

Offline

#11 2012-09-09 23:08:41

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Still learning and stuck with 'date'

Awebb wrote:
zacariaz wrote:

Now that I look at it again, am I correct that

sed 's/ *$//'

might as well be

sed 's/ $//'

?

"Might as well be" as in "there are cases in which it is" and not "it always is a synonym".

I was of course referring to my own situation. wink


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

Board footer

Powered by FluxBB