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. ;)
2017-11-27T15:01:09-0600 Caleb Herbert wrote:
> The '$' (end) of any line that matches '^x '.
>
>