MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
October
Sun Mon Tue Wed Thu Fri Sat
   
9
   
2013
Months
Oct


Wed, Oct 09, 2013 10:39 pm

Inserting text at the beginning and end of lines with vi's regexp

The vi and Vim text editors support the use of regular expressions (abbreviated regex or regexp) for editing files.

In a regular expression ^ signifies the beginning of a line and $ specifies the end of a line. To have an operation apply to multiple lines at once, you can specify a range of lines, e.g. 16,25. To have the operation apply starting with the current line, you can use a period, ., to represent the current line, so .,25 would mean apply the operation to the current line and all subsequent lines up to and including line 25. Or you could specify that the operation should be applied from the current line to the end of the file by using $ to represent the last line in the file, e.g., .,$.

Or you can have an operation apply to every line in a file using %. E.g., the following line would insert 123 at the beginning of every line in a file.

:% s/^/123/

The s indicates a substitute operation will follow and the forward slashes, / delineate the pattern to be replaced and the replacement pattern. In the above case, the ^ indicates the beginning of the line and the replacement pattern is 123.

When you are using / to delineate the patterns, it has a special meaning and if you want to use it in a pattern you have to "escape" its special meaning with an escape character, which is the backslash, \.

E.g. to insert </td> at the end of every line from the current line to line 25, the following regular expression could be used:

.,25 s/$/<\/td>/

Supposing that I wanted to put a <td> at the beginning of each line and a </td> at the end of each line from the current line through line 25. In that case I need to store the characters in between the beginning and end of a line. You can specify text to be stored by using parentheses, ( to mark the beginning of the area on the line to be stored and ) to mark the end. Since a period, ., represents any character and an asterisk, * represents multiple occurrences, ^(.*)$ would store all the characters on the line between the beginning and end of a line. You must also "escape" the ( and ) with the backslash escape character as well. To reuse the characters you have stored you recall them with \1 ("1" is the number one). If you had multiple occurrences of characters enclosed in parentheses, the second instance could be recalled with \2.

The line below would insert a <td> at the beginning of lines 16 through 25 and a </td> at the end of the lines.

:16,25 s/^\(.*\)$/<td>\1<\/td>

If you wished to have the editor prompt you as to whether you wanted the change made on a line, you could add a /c option at the end of the command to the editor. The command below would perform the same action as the one above on every line from line 16 to the end of the file, but would prompt you at each line as to whether the change should be made.

:.,$ s/^\(.*\)$/<td>\1<\/td>/c

You would see each line highlighted one by one as you progressed through the file with the following prompt each time.

replace with <td>\1<\/td> (y/n/a/q/l/^E/^Y)?

Answering y would result in the replacement occurring whereas answering n would result in the line remaining unchanged.

In this case, there is also a simpler means for inserting the table td tags at the beginning and end of each line as below:

s/.*/<td>&<\/td>

Again the .* represents the search pattern, which is every character on the line. The ampersand, &, also has a special meaning; it is the text that matches the search pattern, so putting it between the two tags results in all the text originally on the line remaining on the line. The / at the end of the replacement pattern can be eliminated if there is nothing following it such as a /c.

References:

  1. Adding characters at the start and end of each line in a file
    May 2, 2012
    Stack Overflow
  2. Search and replace
    Vim Tips Wiki

[/software/editors/vi] permanent link

Wed, Oct 09, 2013 8:56 pm

Survey and Correlation Data from 2013 Search Engine Ranking Factors

At 2013 Search Engine Ranking Factors Survey & Correlation Data, I found some interesting information from a survey of search marketers regarding how search engines may rank websites. Many of the factors that may lead to an increase in page ranking didn't surprise me, but some of those that were listed as resulting in a decrease in a page's ranking did surprise me, e.g., the number of characters in a page's title and the use of hyphens in URLs, which I often use. I've factors expected to have negative results below:

Page Has Twitter Card Markup -0.02
# of Large Images (Greater Than Or Equal to 1024X768px) -0.02
Folder Depth of URL (# of Trailing Slashes) -0.02
# of Videos On Page -0.03
Page Contains Google+ Authorship Markup -0.03
# of Google Adsense Slots in The Page -0.03
Domain has Numbers (example123.com) -0.03
# of Hyphens in Domain Name -0.03
Total Area of Adsense Slots on Page -0.04
# of Characters in the Title -0.04
URL Contains Hyphens -0.04
Total Length of the Full Domain (www.subdomain.pld.com) -0.09
URL Length in Characters -0.10
Response Time of Page in Seconds -0.10

[/network/web/search/SEO] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo