In the last paragraph, by "line terminator", I meant either:

- line feed only (LF, 0x0A, "\n"). Default for Unix-compliant operating
  systems such as GNU;

- carriage return only (CR, 0x0D, "\r"). Default for Mac;

- carriage return and line feed (CRLF, 0x0D, "\r\n"). Default for
  Windows.

Most commands assume that you are dealing with LF only, so the special
"$" in regular expressions already mean only "\n". There are other tools
such as `tr' which can do translations (replacements from one terminator
to other) and also `awk' for which by modifying some Awk variables you
can change how it considers input records as terminated (noting that
"records" for Awk can be considered as lines by default).

2017-11-27T21:34:33-0200 Adonay Felipe Nogueira wrote:
> I assume that by "the end" of the line you want to actually capture the
> last character, which ever it is.
>
> Suppose you have a text file like this (the underlines are *also*
> spaces, please correct them):
>
> --8<---------------cut here---------------start------------->8---
> c ^x a1
> y b2
> z c3
> b x d4
> y e5
> z f6
> x abC W-
> a ^x_
> y_
> z_
> e ^x
> x_
> d qwerty ^x azerty aBc
> --8<---------------cut here---------------end--------------->8---
>
>
> Then, assuming you want to match a literal "^x " and are using Basic
> Regular Expressions in GNU `sed', a command such as this:
>
> --8<---------------cut here---------------start------------->8---
> sed -n 's/^.*\^x .*\(.\)$/\1/gp' "file.txt"
> --8<---------------cut here---------------end--------------->8---
>
>
> Would print:
>
> --8<---------------cut here---------------start------------->8---
> 1
> c
> --8<---------------cut here---------------end--------------->8---
>
>
> If you instead want "^" to be considered as special in "^x ", this:
>
> --8<---------------cut here---------------start------------->8---
> sed -n 's/^x .*\(.\)$/\1/gp' "file.txt"
> --8<---------------cut here---------------end--------------->8---
>
>
> Would display this:
>
> --8<---------------cut here---------------start------------->8---
> -
> --8<---------------cut here---------------end--------------->8---
>
>
> The `-n' option in `sed' tells it to print only when asked to, the
> `s/regex/subst/flags' looks for a match of regular expression and
> replaces it, the "g" flag makes it replace all matches, and "p" prints
> the line if a match was found.
>
> Notice however that the captures made so far (of the last character)
> don't overlap with the controlling match ("^x "), that is, so far a
> string such as "x " wouldn't have the space captured. As far as my
> limited knowledge goes, in cases where you have to do a second match
> based on the existance of another match, it's more readable to use *two*
> regular expressions (there are some regular expression standards that
> allow this nesting at the expense of human readability, but I'm still
> not that good on these ones).
>
> The commands bellow use *two* regular expressions, for literal "^x " and
> for the special "^" in "^x ", respectively:
>
> --8<---------------cut here---------------start------------->8---
> sed -n '/\^x / { s/^.*\(.\)$/\1/gp }' "file.txt"
> sed -n '/^x / { s/^.*\(.\)$/\1/gp }' "file.txt"
> --8<---------------cut here---------------end--------------->8---
>
> Unless an address is given, `sed' applies commands to all lines. The
> /regex/ is an address type that selects lines matching a regular
> expression. After the address comes a command, the "{" and "}" are there
> for readability (but are very important if you would want to apply more
> commands to the same address).
>
> Yet again I must remind you that some programming languages have
> different regular expression standards, and so far I don't know which
> one you want.
>
> Finally, if you want to capture the line feed literally, then this can
> be more tricky. Personally, I would test how the file's first line ends
> and apply the same for the future lines I want to write.
>
> I hope this helps. ;)

Reply via email to