how to remove values based on space delimiter

1 visualizzazione (ultimi 30 giorni)
the values present in my file is
f 1 2 3
f 1 2 3
f 1 2 3 4
f 2 3 4
f 4 5 6 7
i have to read every line and check for 4 values separated by space
ie f 1 2 3
if that line has 5 values the last value should get removed
ie
my output should be
f 1 2 3
f 1 2 3
f 1 2 3
f 2 3 4
f 4 5 6
does any built in is there ..pls help....thanks in advance

Risposta accettata

Walter Roberson
Walter Roberson il 24 Dic 2012
S = fileread('YourFileName.txt'); %read file
NewS = regexprep(S, '(?m)(?-s)^(\w+\s+\w+\s+\w+).*', '$1'); %do edit
fid = fopen('YourNewFile.txt, 'wt'); %write out new version
fwrite(fid, NewS);
fclose(fid);
  3 Commenti
Sharen H
Sharen H il 24 Dic 2012
if the values are like this
f 1\2 2\3 2\4 4\5
what changes should i ,make
Walter Roberson
Walter Roberson il 24 Dic 2012
'(?m)(?-s)^(\w+\s+[0-9\\]+\s+[0-9\\]+).*'
This presumes that the first thing on the line will not be of the same form, and that the other values only have digits and backslash.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by