In: , ,
On: 2004 / 01 / 25 Viewed: 22797 times

This bash script extracts a random line from a given (text) file. This is a cheap "fortune" replacement. Goes well with a funny quotes file in you .bashrc :)

CODE:
  1. #!/bin/bash
  2.  
  3. # randomline : extracts a random line from a text file
  4. # Ozh, ozh@planetquake.com
  5.  
  6. if [ -z $1 ] ; then
  7. echo "Usage : $0 <file>"
  8. exit 0
  9. fi
  10.  
  11. NB_LINES=$(expr $(wc -l $1 | sed -e 's/ *//' | cut -f1 -d " "))
  12.  
  13. NB_RAND=0
  14. while [ "$NB_RAND" -eq 0 ]
  15. do
  16. NB_RAND=$(expr $RANDOM \% $NB_LINES)
  17. done
  18.  
  19. sed -n "${NB_RAND}p;${NB_RAND}q" $1

Related posts

Metastuff

This entry "Random Line" was posted on 25/01/2004 at 10:12 pm and is tagged with , ,
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site

No Comment yet

No comments yet. You could get "First Post!" which would bring you fame, luxury cars and chicks.

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

 « Geek code 
 Better update » 
Close
E-mail It