フィルターのクリア

Exporting matched lines from a text file

2 ビュー (過去 30 日間)
jgillis16
jgillis16 2015 年 8 月 12 日
編集済み: Cedric 2015 年 8 月 12 日
I am trying to export the lines in a text file that contain '~' in column 8 to another text file. I have written the code below:
content = fileread('mwithrm21.txt');
linestocopy = regexp(content, '^([^|]*\|){8}~\.*$', 'match', 'dotexceptnewline', 'lineanchors');
newfile = fopen('LFunclassified.txt', 'wt');
fprintf(newfile, strjoin(linestocopy, '\n'));
fclose(newfile);
I don't understand why it is returning a blank file as the export. Most likely a problem in the linestocopy command, but I don't know what else to modify. My file is also attached.

採用された回答

Cedric
Cedric 2015 年 8 月 12 日
編集済み: Cedric 2015 年 8 月 12 日
Because your file contains no new line characters (\n = ASCII 10) but only carriage return characters (\r = ASCII 13). After you read the file, convert \r to \n for testing:
content = fileread( 'mwithrm21.txt' ) ;
content(content==13) = 10 ;
Then you should work on the code that produces the txt file if you can, and use \n. Finally, as Per mentions, you have 8 columns so you have max 7 separators.
Finally, you may be able to simplify your pattern, for something like
'^(.*?\|){7}~.*$'
but you'll have to test it.
  2 件のコメント
jgillis16
jgillis16 2015 年 8 月 12 日
And that did the trick. Thanks both of you!
Cedric
Cedric 2015 年 8 月 12 日
編集済み: Cedric 2015 年 8 月 12 日
The code that you wrote under Per solution should work if you modify the number of columns appropriately and the call to STRSPLIT with '\r'.
If you don't understand it, look at my comment in this thread (I think that you forgot about it).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by