Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

adding lines to existing file

mariospapa
Beginner
2,208 Views
Hi all!

I have a text file with some data (file1) and i want to create a clone of it (file2) but with some additional lines. How I can do this? Is there any way to shift the lines?


Example files with line numbering are:
---------------
File1.dat
---------------
1: 0 1 2 3 4
2: 1 2 3 4 5
3: 2 3 4 5 6
4: 6 7 8 9 10


I want to create File2 like this:
---------------
File2.dat
---------------
1: 0 1 2 3 4
2: 1 2 3 4 5
3: * * *
4: 2 3 4 5 6
5: 6 7 8 9 10
0 Kudos
7 Replies
anthonyrichards
New Contributor III
2,208 Views
Use a text editor.
0 Kudos
mariospapa
Beginner
2,208 Views
I have more than 1000 files to edit!!!!!!!!!!!!! And, I want to insert different data lines in each file!!!!!!
0 Kudos
mecej4
Honored Contributor III
2,208 Views
You can use line-oriented editors, such as edlin (still available in Windows XP-32) or the Unix ed (available for Windows). You also need a system for associating files with lines to enter. Once you have that thought out and ready, batch files will do it.

You can also write a program to do the job, if that is more convenient.

With that many files, you must take precautions to back out if something goes wrong.
0 Kudos
mariospapa
Beginner
2,208 Views
"You can also write a program to do the job, if that is more convenient."



That was my question!!!!!! What is the suitable intrinsic command in fortran to do this manipulation automatically!!!!
0 Kudos
mecej4
Honored Contributor III
2,208 Views
Fortran has no "intrinsic command" to manipulate lines of text, but one can certainly write a program to do the task that you described. If there were intrinsic commands to do every manipulation that one could think of, the language would be so big that it would be impossible to learn.

I should state that your expectation of a manipulation being done automatically, given that you have not yet given an unambiguous specification of the manipulation, is an unreasonable expectation.
0 Kudos
Arjen_Markus
Honored Contributor II
2,208 Views
As the others have indicated, there is no special feature in Fortran to shift lines in a file. Nor in any
programming language that I know of. What you want is quite possible:
Read the file line by line and write the lines into a new file, with some extra write statements in between
for the extra lines you want.

But if you want to automate that, then you will have to have an algorithm by which to decide when to insert
these extra lines and what they should contain.

Here is a trivial example (it assumes the lines are no longer than 80 characters and trailing blanks
are unimportant):

character(len=80) :: line
integer :: ierr, count

open( 10, file = 'file1' )
open( 20, file = 'file2' )

count = 0
do
read( 10, '(a)', iostat = ierr) line
count = count + 1
if ( ierr /= 0 ) exit

if ( count == 3 ) write( 20, '(a)' ) '****** extra line *****'
write( 20, '(a)' ) trim(line)
enddo

close( 10 )
close( 20 )


Regards,

Arjen
0 Kudos
anthonyrichards
New Contributor III
2,208 Views
Pick one of your files at random. How will you recognise whether you have correctly added the required data to it or not?

Are they all text files of data with fixed record lengths?

I must admit I am intrigued as to why you have to do this to 1000's of files. Did someone forget something?
0 Kudos
Reply