user3012926
user3012926

Reputation: 127

Deleting specific rows/lines in a .txt file - MATLAB or Perl

I'm currently writing large data-sets to .txt files proceeding smoothing values with MATLAB using the commands:

hann500=hanning(500);
yy=conv(y,hann500,'same');
fileID = fopen('chrI500bp.txt','w');
for i = 1:length(x)
fprintf(fileID,'%s\t%d\t%d\t%f\n', 'chrI', x(i),x(i),yy(i));
end
fclose(fileID);

Is there a way I can get MATLAB to process the .txt file after creating it to remove all lines containing null-values (0.000....) in the final column (such as those shown below)? The .txt file format is:

chrI    1983    1983    0.000000 - DELETE
chrI    1984    1984    0.000000 - DELETE
chrI    1985    1985    0.000000 - DELETE
chrI    1986    1986    0.000000 - DELETE
chrI    1987    1987    0.000000 - DELETE
chrI    1988    1988    0.000000 - DELETE
chrI    1989    1989    0.000000 - DELETE
chrI    1990    1990    0.000000 - DELETE
chrI    1991    1991    0.000000 - DELETE
chrI    1992    1992    0.000039
chrI    1993    1993    0.000157
chrI    1994    1994    0.000354
chrI    1995    1995    0.000629
chrI    1996    1996    0.000983
chrI    1997    1997    0.001415
chrI    1998    1998    0.001925
chrI    1999    1999    0.002514

Or is there a way to do this in Perl?

Upvotes: 1

Views: 2831

Answers (3)

brianadams
brianadams

Reputation: 233

any reasons why you can't do it in Matlab? this line:

fprintf(fileID,'%s\t%d\t%d\t%f\n', 'chrI', x(i),x(i),yy(i));

possible to check yy(i) first for null values before fprint-ing. use a Matlab if/else statement.

Upvotes: 0

Toto
Toto

Reputation: 91375

I'd do this way:

perl -ane '$F[3]!=0 && print' in.txt

Have a look at perlrun

-a turns on autosplit,
-n adds a while loop arround
so this oneliner is the same as:

while(<>) {
    @F = split(' ');
    print $_ if [$F3] != 0;
}

Upvotes: 1

simbabque
simbabque

Reputation: 54323

You can do it on the command line with grep. Use -v to invert your search. It will return all lines that do not contain the pattern, which is your bunch of zeros and the EOL. Write it to the same file (or another one) and you're all set.

grep -v '0.000000$' foo.txt >foo.txt

If you want to do it in Perl, use this one-liner, which does the same:

perl -n -e 'next if m/0\.000000$/;print' foo.txt

It uses -n to wrap a while (<>) { ... } around your program. The program is the -e, which skips lines with the zeroes and prints the others. Add the -i option if you want it in the same file.

Upvotes: 2

Related Questions