Other Vim and sed not recognizing escape characters

I hope I am putting this in the area.

I am running FreeBSD 10.3 x86_64 ZFS with fully encrypted HDD.

I want to put a CR-LF after every uppercase Q followed by a number, i.e. Q4, Q75, Q453,

In vim, I have tried:

:%s/Q[0-9]+/&\r/g

As well as several variations.

:%s/Q\d\d\d/&\r/g

Vim will never find \d.

In sed I have tried:

$ cat file.txt | sed -r 's/Q[0-9]+/&\r/'

But instead of a CR-LF, I am getting a literal "r".

I have already worked around the problem. I did this substitution in vim:

:%s/Q[0-9]+/&ZZZZZ/g

Then I loaded the file into pluma, and replaced ZZZZZ with '\n'. Still I would like to know why escape characters are not working in vim or sed. Am I doing something wrong? Or is there something wrong with my system?
 
Please try in vim :%s/Q[0-9]\+/&\r/g. I have no idea why the + sign must be masked by a backslash.

I tried that. I get the message "No match found."

In the following file:

Code:
Q123 ksfjslafklas
Q345 skladfjasklf
Q127 skdfjasl;dfj

I have tried :%s/Q\d\d\d/&\r/g and I also get the message "No match found."
~
 
Might have made some progress. Apparently the vi that comes with the standard install is not vim.

Still, I think even standard vi would have worked with some of those commands.

I installed vim and now the command :%s/Q\d\d\d/&\r/g works.

sed still does not work. I could not re-install sed.
 
Dear walterbyrd,
with your example file and :1,$s/Q[0-9]\+/&\r/g I get
Code:
Q123
 ksfjslafklas
Q345
 skladfjasklf
Q127
 skdfjasl;dfj
The part 1,$ defines to apply the substitution on the complete file. I have guessed that you file is build up of one line only. Without 1,$ it is just the current line. The strage thing is that I do not find the simple pattern Q[0-9]+ with nvi.
 
The strage thing is that I do not find the simple pattern Q[0-9]+ with nvi.
re_format(7) gives an explanation.
Code:
     Obsolete (“basic”) regular expressions differ in several respects.  ‘|’
     is an ordinary character and there is no equivalent for its functional‐
     ity.  ‘+’ and ‘?’ are ordinary characters, and their functionality can be
     expressed using bounds (‘{1,}’ or ‘{0,1}’ respectively).
Now seek as /Q[0-9]\{1,\} already matches in nvi but with more typing. But I have no idea how to add a newline with nvi.

If the pattern is always as the example file you could replace the white spaces by \r using tr(1). Otherwise textproc/gsed as proposed by SirDice might help. In the base system should be awk(1) which should lead to a solution. The gnu counterpart is lang/gawk.

Nevertheless I would be happy if someone has a solution how to add a newline with nvi.
 
This works with awk(1) assuming tmp/file has the content as in post #3.
Code:
$ cat /tmp/file |awk '{sub("Q[0-9]+","&\n");print}'
with the same output as in post #7 using editors/vim.
:%s/Q[0-9]+/&\r/g
I thank you for the question because I understand now what & means. A textbook I have states that it is (just) useful to save typing work. Therefore I have never tried it and have had no idea that it works on patterns, too. It requires by far less typing than using the expression using braces. :beer::). Cheers!
 
But I have no idea how to add a newline with nvi.
You can use literal [RETURN] or [CTRL-M] (type [CTRL-V] first to escape them)

example:
Code:
:%s/RETURN/^M/g

The [CTRL-M] above typed in by sequence: [CTRL-V][CTRL-M]
I know it's char(0x13) or CR, but nvi treat them as ^J or linefeed. And I'm happy with that.
 
In sed I have tried:

$ cat file.txt | sed -r 's/Q[0-9]+/&\r/'

But instead of a CR-LF, I am getting a literal "r".

I have already worked around the problem. I did this substitution in vim:

No need a workaround, as with nvi, you can do search/replacement with sed using literal character, [\][RETURN] for linefeed (backslash escaped)
Code:
sed -E 's/Q[0-9]+/&\
/g' file.txt
When using doublequotes, the backslash itself must be escaped
Code:
sed -E "s/Q[0-9]+/&\\
/g" file.txt
Backslashed shortcuts: \r \t \n etc. are extensions.
Contrary to popular beliefs, it had nothing to do with regular expression (RE).
See http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html
 
Back
Top