[SOLVED] To skip lines from the beginning of a file using FORTRAN commands
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
To skip lines from the beginning of a file using FORTRAN commands
Using
READ (8, FMT= '(A1)') dummy
READ (8, FMT= '(A2)') dummy
READ (8, FMT= '(A3)') dummy etc. [8 - file that is being read]
it is possible to skip header lines of a selected input file.
However, I need to skip 95 header lines (1 to 95) and using the above method is not a very practical idea because I need to go on till (A95).
My question is, would there be a shorter way to do the same thing?
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
GNU bash 3.2.39
gfortran GNU Fortran (Debian 4.3.2-1.1) 4.3.2
-----
Input data file data1:
a
b
c
d This is a long line
e
f
g
h
i
j
-----
Fortran source:
program f1
! @(#) f1 Demonstrate Fortran-90.
character*1 dummy
character*80 line
integer :: i, limit
data i, limit / 1, 5 /
open(8,file="data1")
do
if ( i > limit ) exit
read(8,'(a1)') dummy
write(*,*) " Iteration ",i," line skipped ",dummy
i = i + 1
end do
read(8,'(a)') line
write(*,*) " Line after skip is ", line
stop "f1"
end
-----
Results for skipping headers:
Iteration 1 line skipped a
Iteration 2 line skipped b
Iteration 3 line skipped c
Iteration 4 line skipped d
Iteration 5 line skipped e
Line after skip is f
STOP f1
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
GNU bash 3.2.39
gfortran GNU Fortran (Debian 4.3.2-1.1) 4.3.2
-----
Input data file data1:
a
b
c
d This is a long line
e
f
g
h
i
j
-----
Fortran source:
program f1
! @(#) f1 Demonstrate Fortran-90.
character*1 dummy
character*80 line
integer :: i, limit
data i, limit / 1, 5 /
open(8,file="data1")
do
if ( i > limit ) exit
read(8,'(a1)') dummy
write(*,*) " Iteration ",i," line skipped ",dummy
i = i + 1
end do
read(8,'(a)') line
write(*,*) " Line after skip is ", line
stop "f1"
end
-----
Results for skipping headers:
Iteration 1 line skipped a
Iteration 2 line skipped b
Iteration 3 line skipped c
Iteration 4 line skipped d
Iteration 5 line skipped e
Line after skip is f
STOP f1
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.