# Read from text file and skip some part of text

**URL:** https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961
**Category:** General Usage
**Created:** [February 19, 2019, 1:28am UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961 "2019-02-19T01:28:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Suraj\_Pawar](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@Suraj\_Pawar](https://discourse.julialang.org/u/Suraj_Pawar)
#### Post date: [February 19, 2019, 1:28am UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961/1 "2019-02-19T01:28:50Z")

</div>

I have a text file which has below format  
4 ! nx, ny  
25 !x, y  
I want to read only the integer values and assign it to variables. I want to ignore the whitespace and the text after !. I tried using readline command. But it takes the whole line. How can I read just numbers on each line? Thank you.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 19, 2019, 2:01am UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961/2 "2019-02-19T02:01:44Z")

</div>

You can read the whole line and then do stuff with it then. Is this performance critical where its necessary to not read too much?

---

<div class="post-metadata">

### Author: ![Suraj\_Pawar](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@Suraj\_Pawar](https://discourse.julialang.org/u/Suraj_Pawar)
#### Post date: [February 19, 2019, 2:43am UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961/3 "2019-02-19T02:43:24Z")

</div>

No, it is not performance critical. Do you have any suggestions on how I can split the string into two parts, integer and string (which will be discarded)? I am totally new to Julia. Thank you.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 19, 2019, 2:47am UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961/4 "2019-02-19T02:47:05Z")

</div>

The typical way to do this is to use a regular expressions.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 19, 2019, 3:13am UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961/5 "2019-02-19T03:13:35Z")

</div>

And if you aren’t familiar with regular expressions, [regex101](https://regex101.com/) is your friend. Try a tutorial first [here](https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285).

The way Julia implements regular expressions is very standardized and useful.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [February 19, 2019, 3:33am UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961/6 "2019-02-19T03:33:30Z")

</div>

you could

```julia
fields = split(line, " ")
n = parse(Int, fields[1])

```

or

```julia
m = match(r"^([0-9]+)", line)
if m == nothing
  error("integer not found")
end
n = parse(Int, m[1])

```

Note that i’ve written the regular expression such that the integer value _must_ be at the beginning of the line. if there might be spaces then

```julia
r"^ *([0-9]+)"

```

will work. Generally you want to write regular expressions as “tightly” as possible, otherwise they may match something but not what you were expecting.

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [February 19, 2019, 5:27am UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961/7 "2019-02-19T05:27:07Z")

</div>

Read all lines, split by space, take the first element, parse as integer:

```julia
readlines("sample.txt") .|> s -> parse(Int, split(s, " ")[1])

```

You can also use the dot notation:

```julia
parse.(Int, first.(split.(readlines("sample.txt"), " ")))

```

---

<div class="post-metadata">

### Author: ![Suraj\_Pawar](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@Suraj\_Pawar](https://discourse.julialang.org/u/Suraj_Pawar)
#### Post date: [February 19, 2019, 4:00pm UTC](https://discourse.julialang.org/t/read-from-text-file-and-skip-some-part-of-text/20961/8 "2019-02-19T16:00:19Z")

</div>

Thank you everyone for your help.
