Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-23-2011, 05:33 AM   #1
LQ Newbie
Registered: Jun 2011
Posts: 20

Rep: Reputation: Disabled
Fortran. How to automatically find the starting and ending line of desired variable

I am loading the file in Fortran.
File looks something like this (shown below)
I am interested in Velocity values and not Pressure values.

Is there a way to code in Fortran which finds the staring LINE of Velocity values and ending LINE of values or I have to manually find the lines?

IN this case it should return Starting line : 9 Ending line: 11
PHP Code:
1 2 4
2 3 5
3 4 5
4 4 6
5 5 4

1 2 0 
1 3 0 
4 4 5 
Thanks in advance
Old 06-23-2011, 05:46 AM   #2
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
there are ways to do it bash/perl scripting. grep is another tool to do it.
I am not comfortable with Fortran. I would suggest to move this thread to the Programming section, so that it will get more attention.

Old 06-23-2011, 07:36 AM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Indeed the Fortran language is not so smart in parsing files. Anyway you can do some tricks. The logic here is to read values after the "Velocity" line and to stop reading when it encounters a blank line or the end of the file, right? Here we go:
  CHARACTER(len=100) :: string = ''
  INTEGER :: status
  INTEGER, DIMENSION(3) :: values
  OPEN(UNIT=8, FILE='testfile', IOSTAT=status, STATUS='old')
  DO WHILE ( string /= 'Velocity' )
    count = count + 1
    READ(8,'(A)', IOSTAT=status) string
    READ(8,'(A)', IOSTAT=status) string
    IF ( string == '' .OR. status /= 0 ) EXIT
    READ(string,*) values
    WRITE(*,*) values(1), values(2), values(3)
  CLOSE(UNIT=8, IOSTAT=status)
The first loop reads the file until Velocity is encountered. Then you will begin to read the values: the second loop reads the lines as strings, so that you can test if the string is empty. If it is not empty, you read the string (as an internal file) and put the numbers into the integer array values.
1 members found this post helpful.


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
gnome-terminal is starting up in / konsole and xterm start up in ~ as desired dasy2k1 Linux - General 2 08-02-2010 08:43 PM
Problem in getting the starting and ending of a particular record from a file. vigneshinbox Programming 6 04-02-2009 11:28 PM
Execute desired line from the file say_hi_ravi Programming 2 04-09-2008 07:37 AM
cleanly ending a process from command line stu_mueller Slackware 2 04-05-2008 03:42 PM
Removing Text in a single line starting with one pattern ending on another mgwheeler Programming 13 08-03-2004 04:36 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:51 PM.

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