LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-23-2009, 07:35 PM   #1
Speedy2k
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Rep: Reputation: 0
how-to make sed read 1 random line into a file and parse it ot a variable??


I would like to be able to make a variable read from a file, but this file will have a variable lenght, each line can be a variable, but i would like to be able to make the variable random between all those line, it will be into a bash script and it will be running into a DD-WRT linux distribution, the $RANDOM variable doesn't seem to be working, is there any other way to make sed read a random line?? or any other way?? Thanx a lot!
 
Old 05-23-2009, 07:37 PM   #2
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Best Tool For The Job.

Why not write it in python? This is childs play in that
 
Old 05-23-2009, 08:07 PM   #3
Speedy2k
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Because to use python, i'm gona need to install other software, DD-WRT is a linux distribution for router like linksys, so the space we have to play is really low.
 
Old 05-23-2009, 08:42 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Speedy2k View Post
Because to use python, i'm gona need to install other software,
install other software? like what? To use Python,you only need to install Python.
anyway, if you don't have Python, then do it using normal shell tools. see here
 
Old 05-23-2009, 09:00 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
suppose your file has 150 lines:

num=$(($RANDOM % 150))

sed -n "$nump" filename
 
Old 05-24-2009, 08:23 AM   #6
Speedy2k
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
suppose your file has 150 lines:

num=$(($RANDOM % 150))

sed -n "$nump" filename
I know this one, but the problem i got is that in DD-WRT, the $RANDOM variable doesn't seems to be avaible.
Here is what it does when i do your cammand.

Code:
num=$(($RANDOM % 150))
-sh: syntax error:  % 150
If y only make a echo of $RANDOM, i got a blank line. Is there any other way to activate the $RANDOM variable or another way to make this work? thanx!
 
Old 05-24-2009, 09:05 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
interesting.....Do you have the files /dev/random and /dev/urandom?

Some quick Googling suggests that $RANDOM requires one or both of these.
 
Old 05-24-2009, 11:23 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
What does the router use for a shell. For an embedded device, a different program may be used instead, that is aliased to bash. For example, nash or busybox. It may depend how it was built and how your kernel was built. Installing python may be a no-go, but installing bash might be doable. Another alternative is to write a weak random function yourself if a low quality random function would be acceptable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
[SOLVED] use awk &/or sed to read file 1 line 1 & file 2 line 1 gr8scot Linux - General 6 07-12-2007 08:13 PM
how to read certain line of file, and output to variable? jimmyjiang Red Hat 2 05-01-2007 08:46 AM
Read random line from a file in C introuble Programming 6 06-19-2006 09:37 AM
PHP - read a random line from a file? Erik Thorsson Programming 3 12-10-2004 10:31 AM


All times are GMT -5. The time now is 02:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration