delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2004/11/19/10:17:59

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <F76C9B2DA2FC4C4CA0A18E288BBCBCF71026EB0B@nihexchange24.nih.gov>
From: "Buchbinder, Barry (NIH/NIAID)" <BBuchbinder AT niaid DOT nih DOT gov>
To: "'cygwin AT cygwin DOT com'" <cygwin AT cygwin DOT com>
Subject: RE: Grep and matching end of line (anchoring)
Date: Fri, 19 Nov 2004 10:16:43 -0500
MIME-Version: 1.0
X-IsSubscribed: yes

At Friday, November 19, 2004 7:41 AM, Reini Urban wrote:
> Dalton, Barnaby schrieb:
>> I'm having trouble getting grep to match end of line when used with
>> files/utilities that use DOS linefeeds. For example:
>> 
>> cat files.txt | grep '\.h$'
>> 
>> produces no output. However, if I stick a filter in the middle to
>> change the line endings: 
>> 
>> cat files.txt | perl -pe 's/\r\n/\n/' | grep '\.h$'
>> I get:
>> 
>> file1.h
>> file2.h
>> 
>> as expected.
> 
> pipes are treated as binmode, so they don't convert eol from \r\n to
> \n. without pipe it should work on a textmount:
> 
>      grep '\.h$' files.txt
> or  grep '\.h$' < files.txt
> 
>> Should grep's $ match \r\n or should I expect to have to convert line
>> endings?
> 
> grep's "$" is not expected to do textmode magic if stdin is binmode.
> 
> BTW:
>    cat files.txt | sed 's,\r\n,\n,' | grep '\.h$'
> is simpler.
> 
> Someone might think of a new textmode pipe operator (like a new "t|"),
> but I don't consider that a good idea.
> man bash /REDIRECTION and /Pipelines
> --
> Reini Urban
> http://xarch.tu-graz.ac.at/home/rurban/
 
Assuming that \r never occurs in the middle of a line:
	cat files.txt | tr -d '\r' | grep '\.h$'

This should work whether or not one is on a text mount or for the file has
DOS or Unix line endings:
	cat files.txt | grep -E '\.h^M?$'

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019