how to remove values based on space delimiter

1 Ansicht (letzte 30 Tage)
Sharen H
Sharen H am 24 Dez. 2012
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Dez. 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 Kommentare
Sharen H
Sharen H am 24 Dez. 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 am 24 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by