开发者

Search within a fixed width text file

开发者 https://www.devze.com 2023-04-11 11:20 出处:网络
Consider a fixed with file of IDTYPEYEAR 1Book2000 2Car2007 3Home2005 Is there a way to search within a column? In fact, we need to search within characters x to y of each line by skipping the othe

Consider a fixed with file of

ID   TYPE      YEAR
1    Book      2000
2    Car       2007
3    Home      2005

Is there a way to search within a column? In fact, we need to search within characters x to y of each line by skipping the others. For instance, 5 - 15 for searching for TYPE. I prefer to find a solution in php, but if only possible in a specified language, that wo开发者_StackOverflowuld be OK.


Read each line in turn and search within it with strpos by passing in the $offset parameter to start the search from your column. Then check the return value and ignore a "successful" search if it actually found the result in another column (knowing the width of the target column reduces this to an integer comparison).

Alternatively, you might substr the target column from each line and search only within that, in which case you do perform a possibly needless string operation but on the other hand you cut down on the length of the string to be searched (and there's no need to check if the result was found in another column).

In your shoes, I 'd benchmark it a bit before deciding which approach to take.

0

精彩评论

暂无评论...
验证码 换一张
取 消