# Writing in a file?

**URL:** <https://discourse.julialang.org/t/writing-in-a-file/42306>\
**Category:** General Usage\
**Created:** [June 30, 2020, 2:49pm UTC](https://discourse.julialang.org/t/writing-in-a-file/42306 "2020-06-30T14:49:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JTintern](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@JTintern](https://discourse.julialang.org/u/JTintern)\
**Post date:** [June 30, 2020, 2:49pm UTC](https://discourse.julialang.org/t/writing-in-a-file/42306/1 "2020-06-30T14:49:46Z")

</div>

Hi,

Here’s an overview of my file :

```julia
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 🙂

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 30, 2020, 2:54pm UTC](https://discourse.julialang.org/t/writing-in-a-file/42306/2 "2020-06-30T14:54:37Z")

</div>

> [@JTintern](#):
>
> ```julia
> 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' 
> 
> ```

Not a Julia answer but this is easy using `awk`:

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

```

where `temp.txt` is the name of the file.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 30, 2020, 2:56pm UTC](https://discourse.julialang.org/t/writing-in-a-file/42306/3 "2020-06-30T14:56:44Z")

</div>

You can combine `eachline`, `Iterators.filter` and `startswith`.

---

<div class="post-metadata">

**Author:** ![JTintern](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@JTintern](https://discourse.julialang.org/u/JTintern)\
**Post date:** [June 30, 2020, 2:59pm UTC](https://discourse.julialang.org/t/writing-in-a-file/42306/4 "2020-06-30T14:59:01Z")

</div>

okay thanks !

---

<div class="post-metadata">

**Author:** ![JTintern](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@JTintern](https://discourse.julialang.org/u/JTintern)\
**Post date:** [July 1, 2020, 9:40am UTC](https://discourse.julialang.org/t/writing-in-a-file/42306/5 "2020-07-01T09:40:56Z")

</div>

Actually the lines in my file looks like this :

```julia
[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 :

```julia
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 :

```julia
[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

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 1, 2020, 10:21am UTC](https://discourse.julialang.org/t/writing-in-a-file/42306/6 "2020-07-01T10:21:31Z")

</div>

```julia
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](https://docs.julialang.org/en/v1/manual/networking-and-streams/) for information about I/O

---

<div class="post-metadata">

**Author:** ![JTintern](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@JTintern](https://discourse.julialang.org/u/JTintern)\
**Post date:** [July 1, 2020, 11:59am UTC](https://discourse.julialang.org/t/writing-in-a-file/42306/7 "2020-07-01T11:59:08Z")

</div>

Thank you very much !
