Montag, 18. Februar 2008

How to replace the newline "\n" in emacs?

From time to time we need to replace "\n" with other symbols (like ",") in a plain text file, in the scenario, for example, where a input box need a list of gene names separated by "," while we have a csv file having several columns and gene names are one of them.

If familiar with awk, this task can be solved with the following syntax

awk 'BEGIN{ORS=","}{print $1}' YourFileName.csv

Here the ORS(output record separator) specifies the separator while OFS specifies the symbol between more than one items.

But what if we knew nothing about ORS or OFS? then first we could get a gene name file separated by new line easily:

awk '{print $1}' YourFileName.csv > GeneNames.csv

And then we could replace all the new line separators (in Unix/Linux "\n" by default). But unluckily if you try

M-x replace-regex "\n" ,

You will find nothing changed. M-x replace-regex notices the user, with "Note: `\n' here doesn't match a newline; to do that, type C-q C-j instead", if you enter `\n' in the search regular expression (but not in the replacement regular expression).

That is

M-x replace-regex C-q C-j ,