2017-05-13 131 views
0

我有多個數據文件,有時候我想刪除其中不必要的額外數據行,比如說從行8000到行最後一行。我想爲所有包含在.txt文件中的數據文件執行此操作。我曾經手動做過,但經常這樣做是一項乏味的工作。 我已經合併和修改的發現我在網上的一些代碼,但我有兩個問題吧:在Matlab中:如何在多行數據文件中刪除從行xxx到文件尾部的行範圍

  1. 我不知道如何設置的範圍內,從而使代碼線8000後刪除所有行。直到數據結束爲止所以我假設要刪除的行數是一些很大的數字(1000)。但這不是一個好的解決方案,因爲數據文件中的行總數可能超過9000.
  2. 第二件事是我無法複製或移動創建的臨時文件,其中包含沒有額外行的新數據以取代原始文件:(我發現臨時文件夾中的通用唯一標識符(UUID)與「outfile」中的通用唯一標識符(UUID)不同,例如Matlab中的outfile的UUID代碼爲tp58460076_aaad_4621_b2ac_c7036febc3f0,並且在temp文件夾中是tp30ade3f8_3abc_4e00_a2ef_26d67c5f836e我已經使用在Matlab

兩個copyfilemovefile如果某人有一個替代的解決方案或知道什麼是與此代碼腳麻,請^ h ELP!

close all 
clear 
clc 
% Specify the folder where the files live. 
myFolder = 'C:\Users\Emma\Data'; 
% Check to make sure that folder actually exists. Warn user if it doesn't. 
if ~isdir(myFolder) 
    errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder); 
    uiwait(warndlg(errorMessage)); 
    return; 
end 
% Get a list of all files in the folder with the desired file name pattern. 
filePattern = fullfile(myFolder, '*.txt'); % Change to whatever pattern you need. 
theFiles = dir(filePattern); 

for k = 1 : length(theFiles) 
    baseFileName = theFiles(k).name; 
    fullFileName = fullfile(myFolder, baseFileName); 
    fprintf(1, 'Now reading %s\n', fullFileName); 
%***************************** 
first_line_to_delete = 8003; 
num_lines_to_delete = 1000; 
infilename = fullFileName; 
[pathstr, file, ext] = fileparts(infilename); 
backfile = fullfile(pathstr, [file '.bak']); 
if strcmp(infilename, backfile) 
    error('I refuse to edit a backup file! Nothing has been changed.'); 
end 

outfile = tempname; %a temporary file in TMP directory 
fin = fopen(infilename, 'r'); 
if fin < 0; error('Input file does not exist'); end 
fout = fopen(tempname, 'w'); 
if fout < 0 
    fclose(fin); 
    error('Could not open temporary output file'); 
end 

%read lines before the one to be deleted and write them to output 
for K = 1 : first_line_to_delete - 1; 
    inline = fgets(fin); 
    if ~ischar(inline); break; end; %end of file? 
    fwrite(fout, inline); 
end 
for K = 1 : num_lines_to_delete; 
    if ~ischar(inline); break; end %in case EOF 
    inline = fgets(fin); %and do nothing with it 
end 
%copy all remaining input lines to output file 
while ischar(inline) 
    inline = fgets(fin); 
    if ischar(inline) %not if we hit EOF 
    fwrite(fout, inline); 
    end 
end 
fclose(fin); 
fclose(fout); 

%we did the copying and have a file with the desired 
%result. Now put it in the proper place 
[status,message,messageId] = copyfile(infilename, backfile, 'f'); %Emma mod 
if ~status 
    if strcmp(infilename, backfile) 
    fprintf(2, 'Good thing your programmer is paranoid about people overriding\nsanity checks, because something went wrong and you nearly lost your file!\n'); 
    else 
    delete(backfile); 
    end 
    error('Could not rename file to .bak, file left untouched'); 
else 
    [status,message,messageId] = copyfile(fullfile(outfile, [myFolder, infilename]), 'f'); 
    if ~status 
    error(['Could not rename temp file to original name, original moved to ', backfile]); 
    end 
end 
%************************** 
end 
+0

http://stackoverflow.com/questions/19017994/how-do-i-limit-or-truncate-text-file逐數的線。否則,只需刪除'for K = ...'循環和後續的'while ischar ...'循環。 – beaker

+0

@beaker非常感謝你!一行的shell腳本替換了這個冗長的Matlab代碼:)幸運的是,我可以訪問一些終端! – Emma

回答

0

回答這個問題是下面的命令:

sed -i '8003,$ d' *.txt