LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   reading and writing random line from file (https://www.linuxquestions.org/questions/linux-software-2/reading-and-writing-random-line-from-file-645434/)

mcbenus 05-28-2008 04:36 PM

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

pinniped 05-28-2008 06:21 PM

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.

chrism01 05-28-2008 08:01 PM

2 Qns.

1. is this homework?

2. are you programming in shell or C (ie which lang)?

mcbenus 05-29-2008 06:32 AM

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 (Post 3167577)
2 Qns.

1. is this homework?

2. are you programming in shell or C (ie which lang)?


mcbenus 05-30-2008 04:07 PM

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 (Post 3167501)
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.


mcbenus 06-30-2008 10:36 AM

clarifications
 
Quote:

Originally Posted by pinniped (Post 3167501)
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

chrism01 06-30-2008 09:39 PM

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)


All times are GMT -5. The time now is 06:21 PM.