View previous topic :: View next topic |
Author |
Message |
schrippe Guru
![Guru Guru](/images/ranks/rank_rect_3.gif)
![](images/avatars/gallery/6th Day/movie_the_6th_day_adam_gibson.gif)
Joined: 03 Mar 2004 Posts: 556 Location: Mülheim
|
Posted: Wed Aug 17, 2005 1:06 pm Post subject: per random immer verschiedene zeilen eines files ausgeben? |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
slick Bodhisattva
![Bodhisattva Bodhisattva](/images/ranks/rank-bodhisattva.gif)
![](images/avatars/155298905545589d9986ab5.gif)
Joined: 20 Apr 2003 Posts: 3495
|
Posted: Wed Aug 17, 2005 1:23 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
schrippe Guru
![Guru Guru](/images/ranks/rank_rect_3.gif)
![](images/avatars/gallery/6th Day/movie_the_6th_day_adam_gibson.gif)
Joined: 03 Mar 2004 Posts: 556 Location: Mülheim
|
Posted: Wed Aug 17, 2005 1:31 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
slick Bodhisattva
![Bodhisattva Bodhisattva](/images/ranks/rank-bodhisattva.gif)
![](images/avatars/155298905545589d9986ab5.gif)
Joined: 20 Apr 2003 Posts: 3495
|
Posted: Wed Aug 17, 2005 1:36 pm Post subject: |
|
|
bei grep gabs doch auch den Parameter -m für die Anzahl der Matches wenn mich nicht alles täuscht... |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
toskala Advocate
![Advocate Advocate](/images/ranks/rank-G-1-advocate.gif)
![](images/avatars/15247838633ff9618bdf5b6.gif)
Joined: 14 Dec 2002 Posts: 2080 Location: hamburg, germany
|
Posted: Wed Aug 17, 2005 3:59 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
toskala Advocate
![Advocate Advocate](/images/ranks/rank-G-1-advocate.gif)
![](images/avatars/15247838633ff9618bdf5b6.gif)
Joined: 14 Dec 2002 Posts: 2080 Location: hamburg, germany
|
Posted: Wed Aug 17, 2005 4:00 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
Anarcho Advocate
![Advocate Advocate](/images/ranks/rank-G-1-advocate.gif)
![](images/avatars/1030393113423afb9086043.jpg)
Joined: 06 Jun 2004 Posts: 2970 Location: Germany
|
Posted: Wed Aug 17, 2005 4:08 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
schrippe Guru
![Guru Guru](/images/ranks/rank_rect_3.gif)
![](images/avatars/gallery/6th Day/movie_the_6th_day_adam_gibson.gif)
Joined: 03 Mar 2004 Posts: 556 Location: Mülheim
|
Posted: Wed Aug 17, 2005 4:54 pm Post subject: |
|
|
@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 |
|
![](templates/gentoo/images/spacer.gif) |
Fauli l33t
![l33t l33t](/images/ranks/rank_rect_4.gif)
![](images/avatars/9342013042b57afb44a90.png)
Joined: 24 Apr 2004 Posts: 760 Location: Moers, Germany
|
Posted: Wed Aug 17, 2005 8:42 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
schrippe Guru
![Guru Guru](/images/ranks/rank_rect_3.gif)
![](images/avatars/gallery/6th Day/movie_the_6th_day_adam_gibson.gif)
Joined: 03 Mar 2004 Posts: 556 Location: Mülheim
|
Posted: Thu Aug 18, 2005 7:55 am Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
|