Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
per random immer verschiedene zeilen eines files ausgeben?
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German)
View previous topic :: View next topic  
Author Message
schrippe
Guru
Guru


Joined: 03 Mar 2004
Posts: 556
Location: Mülheim

PostPosted: Wed Aug 17, 2005 1:06 pm    Post subject: per random immer verschiedene zeilen eines files ausgeben? Reply with quote

hallo,

ich habe nen File wo URLs drinstehen. jetzt suche ich einen Befehl mit dem ich nur genau diese zeile ausgebe. die zeilennummer wird vonm bashscript per random generiert.

mir fällt kein befehl ein, der das kann.

thx

ben
_________________
for i in $(seq 1 565); do echo 'A$i: entweder rechts fahren oder rechts überholen dürfen!';done
Back to top
View user's profile Send private message
slick
Bodhisattva
Bodhisattva


Joined: 20 Apr 2003
Posts: 3495

PostPosted: Wed Aug 17, 2005 1:23 pm    Post subject: Reply with quote

Wenns nicht unbedingt pure bash sein muß:
http://www.nickles.de/go!linux/faq/index.php3?cmd=lt&t=180

EDIT: blöde URL, wird leider nicht korrekt verlinkt...
Back to top
View user's profile Send private message
schrippe
Guru
Guru


Joined: 03 Mar 2004
Posts: 556
Location: Mülheim

PostPosted: Wed Aug 17, 2005 1:31 pm    Post subject: Reply with quote

ja danke!!

hatte schon das hier probiert:
Code:
cat -n apache2_url.list |grep $((RANDOM %= 5299))


doch wenn man es zu oft aufruft, gibt es mehr als eine zeile gleichzeitig aus.

da ist deins schon besser.
_________________
for i in $(seq 1 565); do echo 'A$i: entweder rechts fahren oder rechts überholen dürfen!';done
Back to top
View user's profile Send private message
slick
Bodhisattva
Bodhisattva


Joined: 20 Apr 2003
Posts: 3495

PostPosted: Wed Aug 17, 2005 1:36 pm    Post subject: Reply with quote

bei grep gabs doch auch den Parameter -m für die Anzahl der Matches wenn mich nicht alles täuscht...
Back to top
View user's profile Send private message
toskala
Advocate
Advocate


Joined: 14 Dec 2002
Posts: 2080
Location: hamburg, germany

PostPosted: Wed Aug 17, 2005 3:59 pm    Post subject: Reply with quote

schrippe wrote:

hatte schon das hier probiert:
Code:
cat -n apache2_url.list |grep $((RANDOM %= 5299))



das liegt daran, dass du für grep nicht eindeutige indizes verwendest.

also:

file sieht wohl so aus:

1 dfasd
2 asdfasd
3 asdfasd
4 fasdfasd
5 afasd
55 adsfasdf

wenn du nun $((RANDOM %= 6)) hast, und dann z.b. nach der 5 grepst, dann bekommst du sowohl das result von 5 als auch von zeile 55 als return value.
_________________
adopt an unanswered post
erst denken, dann posten
Back to top
View user's profile Send private message
toskala
Advocate
Advocate


Joined: 14 Dec 2002
Posts: 2080
Location: hamburg, germany

PostPosted: Wed Aug 17, 2005 4:00 pm    Post subject: Reply with quote

slick wrote:
bei grep gabs doch auch den Parameter -m für die Anzahl der Matches wenn mich nicht alles täuscht...


jopp so gehts:

Code:
cat foo | grep -m 1 $((RANDOM %= n))


cheerios,
toskala
_________________
adopt an unanswered post
erst denken, dann posten
Back to top
View user's profile Send private message
Anarcho
Advocate
Advocate


Joined: 06 Jun 2004
Posts: 2970
Location: Germany

PostPosted: Wed Aug 17, 2005 4:08 pm    Post subject: Reply with quote

Nicht sooo elegant, aber es funktioniert:

Code:
#!/bin/bash

I=1

ZEILE1=`head -n $I $0 | tail -n 1`
I=9
ZEILE9=`head -n $I $0 | tail -n 1`
I=11
ZEILE11=`head -n $I $0 | tail -n 1`
# Zeile 10
# Zeile 11
# Zeile 12
# Zeile 13
# Zeile 14

echo Zeile 1: $ZEILE1
echo Zeile 9: $ZEILE9
echo Zeile 11: $ZEILE11

_________________
...it's only Rock'n'Roll, but I like it!
Back to top
View user's profile Send private message
schrippe
Guru
Guru


Joined: 03 Mar 2004
Posts: 556
Location: Mülheim

PostPosted: Wed Aug 17, 2005 4:54 pm    Post subject: Reply with quote

@toskala:

das file hat doch aber genau 5299 zeilen.

dann dürfte es doch nur genau eine zeile geben, oder?
_________________
for i in $(seq 1 565); do echo 'A$i: entweder rechts fahren oder rechts überholen dürfen!';done
Back to top
View user's profile Send private message
Fauli
l33t
l33t


Joined: 24 Apr 2004
Posts: 760
Location: Moers, Germany

PostPosted: Wed Aug 17, 2005 8:42 pm    Post subject: Reply with quote

Müsste es nicht einfach so funktionieren?
Code:
awk "NR==$(($RANDOM % `wc -l <foo` + 1))" foo

_________________
Do your part to beautify the web! Turn off link underlining!
Back to top
View user's profile Send private message
schrippe
Guru
Guru


Joined: 03 Mar 2004
Posts: 556
Location: Mülheim

PostPosted: Thu Aug 18, 2005 7:55 am    Post subject: Reply with quote

müsste nicht nur, es geht auf jeden fall.

thx
_________________
for i in $(seq 1 565); do echo 'A$i: entweder rechts fahren oder rechts überholen dürfen!';done
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum