LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-26-2011, 02:11 AM   #1
saj
LQ Newbie
 
Registered: May 2011
Posts: 6

Rep: Reputation: Disabled
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?

Thank you and all help is appreciated.
 
Old 05-28-2011, 04:48 PM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

This seems to work. The driver script posts the data file and source code, then compiles and executes:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate compile and execute F-90 code.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C gfortran

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Fortran source:"
cat one.f90

pl " Results for skipping headers:"
rm -f a.out
gfortran -Wall --free-form one.f90
./a.out

exit 0
producing:
Code:
% ./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
Best wishes ... cheers, makyo
 
1 members found this post helpful.
Old 05-31-2011, 11:51 PM   #3
saj
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you makyo.

It works fine and I only have to put the number of lines I need to skip as; DATA I, limit /1, 97/


Quote:
Originally Posted by makyo View Post
Hi.

This seems to work. The driver script posts the data file and source code, then compiles and executes:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate compile and execute F-90 code.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C gfortran

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Fortran source:"
cat one.f90

pl " Results for skipping headers:"
rm -f a.out
gfortran -Wall --free-form one.f90
./a.out

exit 0
producing:
Code:
% ./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
Best wishes ... cheers, makyo
 
  


Reply



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
Commands to remove particular few lines in the history file cartti Linux - Newbie 1 06-16-2010 12:57 PM
how to skip 5 lines notoriouskingpin Linux - Newbie 2 03-12-2009 01:09 PM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 10:40 AM
Delete specific lines of a file beginning with a certain letter docaia Programming 4 08-24-2008 11:04 PM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration