フィルターのクリア

Need to remove '~' in column 5 of text file

1 回表示 (過去 30 日間)
jgillis16
jgillis16 2015 年 7 月 30 日
コメント済み: jgillis16 2015 年 7 月 30 日
content = fileread('mwithin21rm.txt');
linestocopy = regexp(content, '^([^|]*\|){5}~\|.*$', 'match', 'dotexceptnewline', 'lineanchors');
newfile = fopen('m21rmmorphU.txt', 'wt');
fprintf(newfile, strjoin(linestocopy, '\n'));
fclose(newfile);
I need to remove the '~' in column 4 from each line of the text file. The code above moves those lines into a new text file, but I need to eliminate those lines instead. I have attached the file, mwithin21rm.txt.
  2 件のコメント
Cedric
Cedric 2015 年 7 月 30 日
編集済み: Cedric 2015 年 7 月 30 日
There is no '~' in column 4; seeing your pattern, it is column 5.
jgillis16
jgillis16 2015 年 7 月 30 日
Apologies for the mistake. Thanks for catching it! Do you have any idea on how to proceed?

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

採用された回答

Cedric
Cedric 2015 年 7 月 30 日
編集済み: Cedric 2015 年 7 月 30 日
UPDATE 5:04pm
% - Read original.
content = fileread( 'mwithin21rm.txt' ) ;
% - Match and eliminate lines without pattern matching.
sepId = reshape( strfind( content, '|' ), 18, [] ) ;
match = content(sepId(4,:)+1) == '~' ;
lines = strsplit( content, '\n' ) ;
lines(match) = [] ;
% - Export updated content.
fId = fopen( 'm21rmmorphU.txt', 'w' ) ;
fprintf( fId, strjoin( lines, '\n' )) ;
fclose( fId ) ;
FORMER
If I understand well (in particular about col. 5), you want something like this:
% - Read original.
content = fileread( 'mwithin21rm.txt' ) ;
% - Replacement which avoids pattern matching.
sepId = reshape( strfind( content, '|' ), 18, [] ) ;
match = content(sepId(4,:)+1) == '~' ;
content(sepId(4,match)+1) = '' ;
% - Export updated content.
fId = fopen( 'm21rmmorphU.txt', 'w' ) ;
fwrite( fId, content ) ;
fclose( fId ) ;
PS: I love regular expressions ;-) but this seems to be a case where we can avoid their complexity.
  3 件のコメント
Cedric
Cedric 2015 年 7 月 30 日
Ok, I am updating the answer.
jgillis16
jgillis16 2015 年 7 月 30 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by