Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Sep 2017 04:23:32 +0300
From:      Yuri Pankov <yuripv@gmx.com>
To:        Polytropon <freebsd@edvax.de>, Ernie Luzar <luzar722@gmail.com>
Cc:        "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
Subject:   Re: script code for end-line
Message-ID:  <c8715d2d-cb1f-592b-7541-a556fa5645a7@gmx.com>
In-Reply-To: <20170909030257.d2718c00.freebsd@edvax.de>
References:  <59B332A3.1000205@gmail.com> <20170909030257.d2718c00.freebsd@edvax.de>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, 9 Sep 2017 03:02:57 +0200, Polytropon wrote:
> On Fri, 08 Sep 2017 20:15:31 -0400, Ernie Luzar wrote:
>>
>> I have a file that has blank lines with ^M in position one.
>>
>> I have this  if [ "$end-line" = "^M"]; then
>>
>>
>> Is that the correct way to code that between the quotes?
> 
> That will only match the literal string ^M (^ and M).
> String evaluation and comparison at this low level
> isn't a native skill of sh. There is a way of encoding
> characters as octal values, such as \015 for \r, which
> equals ^M and 0x0D, but /bin/test (which is [) can only
> compare strings.
> 
> Here is a terrible workaround (not tested):
> 
> if [ `echo ${end-line} | od -x | head -n 1 | awk '{ print $2 }'` = "000d" ]; then
> 	... do something ...
> fi
> 
> Check if there is already a tool for what you're trying
> to accomplish (e. g., tr, sed, recode, iconv). ;-)

Actually, you can insert real ^M characters and /bin/test should be able 
to handle them - press ctrl+V ctrl+M.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?c8715d2d-cb1f-592b-7541-a556fa5645a7>