|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|