Writing in a file?

Hi,

Here’s an overview of my file :

Window gained focus --> CreateSurveyDialog = 'Create 3D survey'  -->  frame = 'DataManager' 
Focus gained --> TextField = 'MAP_' --> CreateSurveyDialog = 'Create 3D survey' 
MB1  --> TextField = 'MAP_' --> CreateSurveyDialog = 'Create 3D survey' 
Focus lost --> TextField = '' --> CreateSurveyDialog = 'Create 3D survey' 
MB1  --> JButton 'Pick on map'  --> CreateSurveyDialog = 'Create 3D survey' 
Window gained focus -->  frame = 'GenericFrame' 

I would like to retrieve only the lines that start with “Window” and then write them in a new text file. How can I do that ?

Thank you :slight_smile:

Not a Julia answer but this is easy using awk:

awk '$1=="Window"' temp.txt

where temp.txt is the name of the file.

You can combine eachline, Iterators.filter and startswith.

2 Likes

okay thanks !

Actually the lines in my file looks like this :

[2020/06/18 17:19:44] Window opened -->  frame = 'ToolBox' 
[2020/06/18 17:19:44] MB1  --> Menu item = [WFT Interpretation]  -->  frame = 'ToolBox' 

Here’s what I have done so far :

file = open("Test2.txt") do file
    f = readlines(file)
    for line in f
      if line[23:28]== "Window"
         open("t.txt","w") do file
         write(file,line)
         end
      end
   end
end

I tried what I was advised to do in the posts above but I didn’t succeed. So I tried a simpler syntax for me (probably less efficient).

My problem is that only the last line containing “Window” from the first file is written to the new file.
For example, with the file preview in my first post I get this :

[2020/06/25 17:33:05] Window gained focus -->  frame = 'GenericFrame'

How do I make sure that ALL the lines containing “Window” are written to the new file?

thanks

out=open("t.txt","w")
file = open("Test2.txt") do file
    f = readlines(file)
    for line in f
      if line[23:28]== "Window"
         write(out,line)
		 write(out,"\n")
      end
   end
end
close(out)

See Networking and Streams · The Julia Language for information about I/O

Thank you very much !

1 Like