# Problem parsing a txt file

**URL:** <https://discourse.julialang.org/t/problem-parsing-a-txt-file/60432>\
**Category:** General Usage\
**Created:** [May 2, 2021, 8:00pm UTC](https://discourse.julialang.org/t/problem-parsing-a-txt-file/60432 "2021-05-02T20:00:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)\
**Post date:** [May 2, 2021, 8:00pm UTC](https://discourse.julialang.org/t/problem-parsing-a-txt-file/60432/1 "2021-05-02T20:00:35Z")

</div>

Hello,

I am reading a txt file with this format:

```julia
06/22/2021 {somenthing}
06/22/2021 {somenthing}
06/22/2021 {somenthing so long that goes
to the other line}
06/22/2021 {somenthing}

```

To read this file i am using:

`open("test.txt") do f`

`line = 0`  
`# read till end of file`  
`while ! eof(f) `  
`# read a new / next line for every iteration`  
`s = readline(f)`  
`line + = 1`  
`println( "$s" )`  
`end`  
`end`

Well, what i need is to ensure that each new line starts with a date, not a string as it happens when the text is too long. How can i do that?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 2, 2021, 8:17pm UTC](https://discourse.julialang.org/t/problem-parsing-a-txt-file/60432/2 "2021-05-02T20:17:37Z")

</div>

if you’re just counting the number of “entry” (defined by starting with a date) in that file, you can use a regular expressing to match the dates as `r"^\d{2}/\d{2}/\d{4}"`:

```julia
julia> open("test.txt") do f
          line = 0
          while !eof(f)
              s = readline(f)
              if occursin(r"^\d{2}/\d{2}/\d{4}", s)
                  line += 1
                  println(s)
              end
           end
           println(line)
       end
06/22/2021 {somenthing}
06/22/2021 {somenthing}
06/22/2021 {somenthing so long that goes
06/22/2021 {somenthing}
4

```

---

<div class="post-metadata">

**Author:** ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)\
**Post date:** [May 2, 2021, 8:20pm UTC](https://discourse.julialang.org/t/problem-parsing-a-txt-file/60432/3 "2021-05-02T20:20:09Z")

</div>

Thanks. Iĺl try as soon as i get home. I am not counting the number of entry. I need to parse this file and i need each line starting with a date otherwise my code fails.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 2, 2021, 8:20pm UTC](https://discourse.julialang.org/t/problem-parsing-a-txt-file/60432/4 "2021-05-02T20:20:36Z")

</div>

> [@statspy](#):
>
> I need to parse this file

this is fine, you just need to modify the code such that you treat `nextline` as the same line until you hit a line that starts with a date, just need to modify the loop a bit.

> [@statspy](#):
>
> i need each line starting with a date otherwise my code fails.

then you should fix the file if possible

---

<div class="post-metadata">

**Author:** ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)\
**Post date:** [May 2, 2021, 8:22pm UTC](https://discourse.julialang.org/t/problem-parsing-a-txt-file/60432/5 "2021-05-02T20:22:19Z")

</div>

> [@jling](#):
>
> then you should fix the file if possible

its is possible, the problem is that i get like 20 files like this per week…
