Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to replace number or word to the empty word with only 'SPACE'

Status
Not open for further replies.

ericc111

Programmer
Jan 3, 2010
3
PL
Hi, everyone i have got a problem. I have got a file abc.txt:
13 -- 0 -- 8 -- 0 -- 1
12 -- 5 -- 0 -- 9 -- 0
11 -- 0 -- 0 -- 0 -- 1


everyone number is separate "TAB character -- " (char(9)).
My question is how delete number "0" and replace it a "blank word" 'SPACE' (' ') with keep layout with TABs.
For clarity output should be like that:

13 -- -- 8 -- -- 1
12 -- 5 -- -- 9 --
11 -- -- -- -- 1

Thank you for help.
 
I'd just read it as text

Code:
INTEGER :: i
INTEGER, PARAMETER :: linelength
CHARACTER(LEN=linelength) :: oneline

And change the "0" looking for it

Open the file and loop in it reading line by line
Read the line and change it

Code:
DO i=1,linelength,1
   IF(oneline(i:i)=='0')oneline(i:i)=' '
END DO

Then write the changed line and read on the next one

You may also use the INDEX command, but I don't know exactly how to deal with the second '0' if you've got three "0"'s in one line
 
Let rather any system utility do that work for you.
For example this
Code:
$ sed s/0/\ /g [i]input_file[/i] > [i]output_file[/i]
transform the above input_file to output_file:
Code:
13  --   -- 8 --   -- 1
12  -- 5 --   -- 9 --  
11  --   --   --   -- 1
You can call the above sed utility using Fortran system() function.
Or instead of sed you can use any scripting language that which does the work easily, like Perl, Python, Ruby, Tcl, REXX, ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top