[R] question

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Nov 19 16:48:39 CET 2003


Philippe Glaziou <glaziou at pasteur-kh.org> writes:

> > Er, no, that wasn't the requirement. It's a job for awk or perl, e.g.
> > 
> > #!/usr/bin/perl -n
> > if (/pattern 1/){
> >     $copy = 1;
> >     next;
> > }
> > if (/pattern 2/){
> >     $copy = 0;
> > }
> > print if $copy;
> > 
> > or 
> > 
> > awk '/pattern 1/{copy=1;next};/pattern 2/{copy=0};copy==1' < file.txt > file2.txt
> 
> 
> Peter, I cannot see your point. sed can get rid of any pattern in
> a text file. Fuensanta's example seemed to show that the
> sentences (pattern 1, 2,...) were on separate lines from lines
> containing data, thus my approach. Another one closer to your awk
> example would use:
> 
> sed -e '/pattern 1\|pattern 2\|pattern xyz//g' <file.txt>file2.txt
> 
> Or is this just a perl versus sed versus awk troll?

Data was

2.22 3.45
1.56 2.31
pattern 1
4.67 7.91
3.34 2.15
5.32 3.88
pattern 2

blueberry:~/> sed -e 's/pattern 1\|pattern 2\|pattern xyz//g' < tst.txt
2.22 3.45
1.56 2.31

4.67 7.91
3.34 2.15
5.32 3.88

blueberry:~/> awk '/pattern 1/{copy=1;next};/pattern 2/{copy=0};copy==1' < tst.txt
4.67 7.91
3.34 2.15
5.32 3.88
blueberry:~/> perl -ne 'if(/pattern 1/){$copy=1;next;} if(/pattern 2/){$copy=0;}print if $copy' < tst.txt
4.67 7.91
3.34 2.15
5.32 3.88
blueberry:~/>



The original message started:

"Does anyone know how I can read from a .txt file the lines that are between
two strings whose location is unknown?"


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list