Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang read from file the first 5 rows

For example i have txt file with 10 string of text. How can i read first 5 string of this text with erlang?

Thank you.

like image 380
0xAX Avatar asked Feb 05 '11 05:02

0xAX


3 Answers

Probably you want a combination of file:open/2 and file:read_line/1 with buffering enabled.

A rhyme:

$ cat mary_lamb.txt
Mary had a little lamb,
little lamb, little lamb,
Mary had a little lamb,
whose fleece was white as snow.
And everywhere that Mary went,
Mary went, Mary went,
and everywhere that Mary went,
the lamb was sure to go.

Source file:

$ cat ./read_n_lines.erl
-module(read_n_lines).
-export([read_n_lines/2]).

read_n_lines(Filename,NumLines) ->
     {ok, FileDev} = file:open(Filename, 
          [raw, read, read_ahead]),
     Lines = do_read([],FileDev, NumLines),
     file:close(FileDev),
     Lines.

do_read(Lines, _, 0) ->
     lists:reverse(Lines);
do_read(Lines, FileDev, L) ->
     case file:read_line(FileDev) of
          {ok, Line} ->
               do_read([Line|Lines], FileDev, L - 1);
          eof ->
               do_read(Lines, FileDev, 0)
     end.

raw, in Modes, passed to the file:open/2, allows faster access to a file, because no Erlang process is needed to handle the file.

Sample run:

$ erl
1> c(read_n_lines).
{ok,read_n_lines}
2> Lines = read_n_lines:read_n_lines("./mary_lamb.txt", 5).
["Mary had a little lamb,\n","little lamb, little lamb,\n",
 "Mary had a little lamb,\n",
 "whose fleece was white as snow.\n",
 "And everywhere that Mary went,\n"]
3> length(Lines).
5
4> read_n_lines:read_n_lines("./mary_lamb.txt", 666).
["Mary had a little lamb,\n","little lamb, little lamb,\n",
 "Mary had a little lamb,\n",
 "whose fleece was white as snow.\n",
 "And everywhere that Mary went,\n",
 "Mary went, Mary went,\n",
 "and everywhere that Mary went,\n",
 "the lamb was sure to go."]
5> 

To remove newline from a string, you can use string:strip/1,2,3:

5> lists:map(fun(X) -> string:strip(X, right, $\n) end, Lines).
["Mary had a little lamb,","little lamb, little lamb,",
 "Mary had a little lamb,",
 "whose fleece was white as snow.",
 "And everywhere that Mary went,"]
6>
like image 197
YasirA Avatar answered Nov 14 '22 15:11

YasirA


Another solution, n_times can be used elsewhere:

-module(n_times).

-export([test/0]).

test() ->
  io:format("~p~n", [n_lines("n_times.erl", 5)]).

n_lines(FileName, N) ->
  {ok, FileDev} = file:open(FileName, [raw, read, read_ahead]),
  try
    n_times(fun() -> {ok, L} = file:read_line(FileDev), L end, N)
  after
    file:close(FileDev)
  end.

n_times(F, N) ->
  n_times(F, N, []).

n_times(_, 0, A) ->
  lists:reverse(A);
n_times(F, N, A) ->
  n_times(F, N-1, [F()|A]).
like image 2
Victor Moroz Avatar answered Nov 14 '22 14:11

Victor Moroz


use io module of erlang.

io:read(FD,'').

Where FD is the File handle.

Also please do lookup the erlang doc for the correct syntax.

Here is a rough code

func(FD) ->
case io:get_line(FD,'') of
{ok,text}->
 %%do something,
func(FD);
eof ->
%%exit;
error->
%%quit
end

You can use a counter if you want to process just 10 lines

like image 1
Arunmu Avatar answered Nov 14 '22 13:11

Arunmu