1

I have one tab delimited text file (TXT) with more than 1 million lines(records) and around 400 MB file size

Example

demo    123333    new
demo2    3444442    new
demo4    56666632    old
demo5    455552333   new

So now I want to find out line which 2nd tab is equal to "56666632"

also convert all tab value of that line covert into array like :

data[0]=>demo4
data[1]=>56666632
data[2]=>old

Note : I want without while and foreach because very large number of line in txt file

I am try this code with while loop:

$file = "feed.txt";
$cols = array();
ini_set('auto_detect_line_endings', true);
$fh = fopen($file, 'r');
while (($line = fgetcsv($fh, 1000, "\t")) !== false) 
{ 
  if($line[1]=="56666632")
  {  
  var_dump($line);
  break;
  }
}
  • If you're explicitly precluding the options that allow you to read the file line by line in PHP, then you're probably limited to OS search commands like grep.... but why don't you want to use loops? – Mark Baker May 30 '14 at 13:56
  • This might be helpful http://stackoverflow.com/a/3686287/2045041 – Ehsan May 30 '14 at 13:58
  • `I want without while and foreach because very large number of line in txt file`. This seems wrong to me, you want to do that in line-per-line when you have big files otherwise you'll fall into memory issues. –  May 30 '14 at 14:04

2 Answers2

1

If you're on a Linux box, you could try delegating the job to grep:

$line = shell_exec('grep -P "\t56666632\t" feed.txt');
daiscog
  • 11,441
  • 6
  • 50
  • 62
0

You're going to have a hard time doing this without a foreach or while loop. So hard, in fact, taht I wouldn't even waste time looking for an answer

If you're on a Unix like machine, use grep:

grep -nE \
  '^[^[:space:]]+[[:space:]]+56666632[[:space:]]+[^[:space:]]+' \
  feed.txt \
  | awk -F: '{print $1}'

The regular expression will search for a string which contains, in order:

  • At least one character which is not a white space character
  • At least one white space character
  • The number you're after
  • At least one white space character
  • At least one character which is not a white space character

using an extended regular expression.

DaveyBoy
  • 2,928
  • 2
  • 17
  • 27