Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.fortran

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-2012, 04:56 PM
harishameed_33
Guest
 
Posts: n/a
Default fortran: read / write data file

i am new to this forum and in using fortran, i have an input file which looks like

1 e:1 s:5 n= 128 5 1 52

4 e:10 s:6 n= 38 20 117 132

7 e:141 s:3 n= 1 301 25 3

............... ............... ...............

147 e:19 s:3 n= 127 132 131 126

what i want is to read this input file and write only the numbers from this file in an output file like

1 5 128 5 1 52

10 6 38 20 117 132

141 3 1 301 25 3

............... ...............

19 3 127 132 131 126

someone please guide me regarding this



Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 08-07-2012, 07:22 PM
Aris
Guest
 
Posts: n/a
Default Re: fortran: read / write data file

On Tue, 07 Aug 2012 11:56:47 -0500, harishameed_33 wrote:

> i am new to this forum and in using fortran, i have an input file which
> looks like
>
> 1 e:1 s:5 n= 128 5 1 52
>
> 4 e:10 s:6 n= 38 20 117 132
>
> 7 e:141 s:3 n= 1 301 25 3
>
> .............. ............... ...............
>
> 147 e:19 s:3 n= 127 132 131 126
>
> what i want is to read this input file and write only the numbers from
> this file in an output file like
>
> 1 5 128 5 1 52
>
> 10 6 38 20 117 132
>
> 141 3 1 301 25 3
>
> .............. ...............
>
> 19 3 127 132 131 126
>
> someone please guide me regarding this


This is probably not the easiest way to do it, but if you are continuing
with Fortran, you may want to write your own little library with routines
like readIntAndShift below. The program below takes the blank lines in
your input seriously. It takes standard input, and sends to standard
output.

program test
implicit none
character(72) :: line
integer :: i1,i2,i3,i4,i5,i6,i7
do
read(*,'(A)',end=111) line
call readIntAndShift(i1,line)
call readIntAndShift(i2,line)
call readIntAndShift(i3,line)
call readIntAndShift(i4,line)
call readIntAndShift(i5,line)
call readIntAndShift(i6,line)
call readIntAndShift(i7,line)
write(*,*) i2,i3,i4,i5,i6,i7
read(*,*)
enddo
111 continue

contains

subroutine readIntAndShift(ii,line)
character(*),intent(inout) :: line
integer,intent(out) :: ii
integer :: iPos
iPos = verify(line,'0123456789')
read(line(1:iPos-1),*) ii
line = adjustl(line(iPos)
iPos = scan(line,'0123456789')
line = adjustl(line(iPos)
end subroutine

end program
Reply With Quote
  #3 (permalink)  
Old 08-07-2012, 08:11 PM
glen herrmannsfeldt
Guest
 
Posts: n/a
Default Re: fortran: read / write data file

harishameed_33 <harishameed_33@hotmail.com> wrote:
> i am new to this forum and in using fortran, i have an input file which looks like


> 1 e:1 s:5 n= 128 5 1 52
> 4 e:10 s:6 n= 38 20 117 132
> 7 e:141 s:3 n= 1 301 25 3


(snip)
> what i want is to read this input file and write only the
> numbers from this file in an output file like


It can be done in Fortran, and isn't all that hard, but programs
like sed, or even tr, are more usual for this type of problem.
The exact form depends on what you want to convert to space, and
what goes away.

tr -dc " 0-9" < infile > outfile

might do it. (Delete all characters other than space and digits.)

They are usual unix utilities, available for Win32 in UnxUtils
from sourceforge.

-- glen
Reply With Quote
  #4 (permalink)  
Old 08-08-2012, 06:00 AM
harishameed_33
Guest
 
Posts: n/a
Default RE: fortran: read / write data file

thankx for your replies

@user3

i have tries to run the program but it gives some error... kindly tell me what changes i have to make to run it for my case

@ gah

i have tried to use the command tr , it works but i get all the number in single line , but i want them to be in separate line like this

> 1 5 128 5 1 52
>
> 10 6 38 20 117 132
>
> 141 3 1 301 25 3
>
> .............. ...............
>
> 19 3 127 132 131 126


please tell me what to change to run it successfuly
thankx

--http://compgroups.net/comp.lang.fortran/fortran-read-write-data-file/1539393


Reply With Quote
  #5 (permalink)  
Old 08-08-2012, 07:35 AM
glen herrmannsfeldt
Guest
 
Posts: n/a
Default Re: fortran: read / write data file

harishameed_33 <harishameed_33@hotmail.com> wrote:
> thankx for your replies


> @user3


> i have tries to run the program but it gives some error...
> kindly tell me what changes i have to make to run it for my case


> @ gah


> i have tried to use the command tr , it works but i get all
> the number in single line , but i want them to be in
> separate line like this


>> 1 5 128 5 1 52

(snip)

> please tell me what to change to run it successfuly


Oops, sorry about that:

tr -dc "\n 0-9" < infile > outfile

remove all characters except digits, space, and newline.

Works both with unix and Win32 versions.

-- glen
Reply With Quote
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:09 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.