LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 05-28-2008, 04:36 PM   #1
mcbenus
LQ Newbie
 
Registered: Feb 2007
Posts: 24

Rep: Reputation: 15
reading and writing random line from file


Hello,

In my tcsh script I need to read a random line from a file and write it into a new file. I can open the file with fopen, but don't know how to continue. another restriction is that the line being read has to appear after the second @ symbol (so basically I need to recognize the second @, the end of the file, and randomly pick a line in between).

I saw a previous related post here but it was too advanced for me to understand it. I am new to linux and programming, so please account for that if you are trying to help me.

Any suggestions will be appreciated!

BK
 
Old 05-28-2008, 06:21 PM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Easy:
1. use 'fgets' if you know the length of the longest line - read in 1 line at a time until you find the second '@' - use 'ftell' to get its file position.
2. use 'fseek' to go to the end of the file and read the position with 'ftell'
3. Select a random number from the '@' to the end of file and read backwards until you find a '\n' - then use fgets again to read the line from that position. Alternatively, after finding the start, just use 'fgets' + 'ftell' until you get to the end of file; use teh info from 'ftell' to make a list of line start positions - then select them at random and use 'fgets' to read them.
 
Old 05-28-2008, 08:01 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
2 Qns.

1. is this homework?

2. are you programming in shell or C (ie which lang)?
 
Old 05-29-2008, 06:32 AM   #4
mcbenus
LQ Newbie
 
Registered: Feb 2007
Posts: 24

Original Poster
Rep: Reputation: 15
1. No, it's not homework, it's for academic research
2. if i understood your question, it's a C shell (or tc shell - tcsh).

Quote:
Originally Posted by chrism01 View Post
2 Qns.

1. is this homework?

2. are you programming in shell or C (ie which lang)?
 
Old 05-30-2008, 04:07 PM   #5
mcbenus
LQ Newbie
 
Registered: Feb 2007
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks for the reply. I am trying to get the right syntax and script with the commands you suggested. I may be back with some usage clarification questions...

BK

Quote:
Originally Posted by pinniped View Post
Easy:
1. use 'fgets' if you know the length of the longest line - read in 1 line at a time until you find the second '@' - use 'ftell' to get its file position.
2. use 'fseek' to go to the end of the file and read the position with 'ftell'
3. Select a random number from the '@' to the end of file and read backwards until you find a '\n' - then use fgets again to read the line from that position. Alternatively, after finding the start, just use 'fgets' + 'ftell' until you get to the end of file; use teh info from 'ftell' to make a list of line start positions - then select them at random and use 'fgets' to read them.
 
Old 06-30-2008, 10:36 AM   #6
mcbenus
LQ Newbie
 
Registered: Feb 2007
Posts: 24

Original Poster
Rep: Reputation: 15
clarifications

Quote:
Originally Posted by pinniped View Post
Easy:
1. use 'fgets' if you know the length of the longest line - read in 1 line at a time until you find the second '@' - use 'ftell' to get its file position.
2. use 'fseek' to go to the end of the file and read the position with 'ftell'
3. Select a random number from the '@' to the end of file and read backwards until you find a '\n' - then use fgets again to read the line from that position. Alternatively, after finding the start, just use 'fgets' + 'ftell' until you get to the end of file; use teh info from 'ftell' to make a list of line start positions - then select them at random and use 'fgets' to read them.
I overcame some other obstacles and now ready for this part but need more help please -

I am running the tcsh script in cygwin. The first line of my script is #!/usr/bin/tcsh and I am able to execute it. When I added fopen command to read the content of receptor.mol2 I get an error. The script:

FILE * receptor
receptor = fopen("receptor.mol2","r")

The error:

$ ./fopentest.tcsh
dock.csh: ASCII text, with CRLF line terminators
fopentest.tcsh: a /usr/bin/tcsh\015 script text executable
receptor.mol2: ASCII text
receptor: cannot open `receptor' (No such file or directory)
Badly placed ()'s.

where the first word in each line is a file in the folder.

Can you walk me through the syntax please?

Thanks,
BK
 
Old 06-30-2008, 09:39 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Disclaimer; I've never used tcsh

1.
/usr/bin/tcsh is a shell version eg like bash or ksh

2.
FILE * receptor
receptor = fopen("receptor.mol2","r")

this looks like C code ie a a compilable lang.


ie 2 completely diff langs. You can't mix them like this.
(but see disclaimer above)
 
  


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
struct reading and writing to file ilnli Programming 8 08-20-2007 09:54 AM
writing and reading from a file! sahel Programming 1 12-27-2005 01:33 PM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM
C File reading and writing AbhishekSamuel Programming 3 05-03-2005 03:59 PM
Help reading and writing to file xiste Programming 1 04-15-2005 12:43 AM


All times are GMT -5. The time now is 11:00 PM.

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