Tapatalk

How to add certain character at the end of every odd line?

How to add certain character at the end of every odd line?

1
NewbieNewbie
1

Post7:40 - Mar 06#1

For example, I want to insert @ at the end of odd lines. Currently I'm using the way of holding ctrl and select every odd line manually but it is too time-consuming and very easy to make mistake. 

I have looked at Replace and Select option but neither of them provides with odd lines selction and I do not know how to write regular expression.

Is there any way to do this? Appreciate if detailed example and usage explanation can be given. Thanks.

6,824625
Grand MasterGrand Master
6,824625

Post18:37 - Mar 06#2

There should be known first that a text file is for a find/replace just a stream of characters. There are no lines and line numbers. That interpretation of the character stream is done by UltraEdit but not by the find/replace functions in any text editor. It would require a special feature of the text editor interpreting the character stream as a series of lines to run a find/replace only on lines with an odd or an even line number, or on every third, fourth, fifth … line in a text file.

There can be used a Perl regular expression replace all executed from top of the file with the search expression ^(.*)((?:\r?\n|\r|$)(?:.|\r?\n|\r|$)) and the replace expression \1@\2 to append @ at the end of every odd line.

Explanation:

^ … start every search at the beginning of a line – any line!

() … first capturing/marking group. The string with zero or more characters found by the expression inside the group is referenced with \1 in the replace string to keep that string as is. The first marking group matches everything on an odd line without the newline characters.

.* … any character except newline characters zero or more times greedy. That expression matches everything on an odd line from the beginning to the end of the odd line.

() … second capturing/marking group. The string with 0 to four characters found by the expression inside the group is referenced with \2 in the replace string to keep that string as is.

(?:) … a non-capturing/non-marking group used twice to define an OR expression.

\r?\n … matches an optionally present carriage return and a line feed. That expression matches a DOS/Windows (carriage return + line feed) and a UNIX (just line feed) line termination at the end of an odd line in first non-marking group or the line termination of a blank even line in second non-marking group. ? after \r is here a multiplier and means zero or once.

| … means OR.

\r … matches a MAC (only carriage return) line termination.

$ … means end of line without matching the newline character(s) and also end of searched character stream which is the end of the file. The expression |$ in the first non-marking group is for getting a positive search in case of the last line of the file is an odd line without a line termination while the expression |$ in second non-marking group is for getting a positive search in case of the last line of the file is an odd line with a line termination.

The first non-marking group matches therefore the line termination of an odd line or nothing in case of the last line of the file is an odd line with no line termination.

The second non-marking group is similar to the first one, but it begins with . to match the first character not being a newline character at the beginning of the next even line. But it is possible that the even line is an empty line with no characters at all, just the line termination, or there is no more even line after the last odd line in the file. For that reason the second non-marking group matches also on finding no newline character at the beginning of the even line either the DOS/UNIX/MAC line termination of the even line or nothing on end of the file.

The Perl regular expression replace all could be run also from the beginning of the third, fifth, seventh … line in a text file to keep a possible header as is.

It is of course also possible to start the Perl regular expression replace all from the beginning of the second line to append @ at the end of every even line.

The expression can be simplified on knowing that the last line of the processed file has always a line termination by using in this case as search expression ^(.*)((?:\r?\n|\r)(?:.|\r?\n|\r)) with both |$ removed from the two OR expressions in the two non-marking groups.

The expression can be even more simplified on knowing that the processed file has always DOS/Windows line terminations including the last line in the file by using in this case as search expression: ^(.*)(\r\n(?:.|\r\n))

The most simple search expression ^(.*)(\r\n.) for appending @ at the end of every odd (or every even line on starting the replace at the beginning of second line) works only for text files with DOS/Windows line terminations with last line having always also a line termination and with no empty lines in the text file.