![読みやすいように強調表示しながら、区切り文字で区切られた値のファイルを Emacs で表示するにはどうすればよいですか?](https://rvso.com/image/1303447/%E8%AA%AD%E3%81%BF%E3%82%84%E3%81%99%E3%81%84%E3%82%88%E3%81%86%E3%81%AB%E5%BC%B7%E8%AA%BF%E8%A1%A8%E7%A4%BA%E3%81%97%E3%81%AA%E3%81%8C%E3%82%89%E3%80%81%E5%8C%BA%E5%88%87%E3%82%8A%E6%96%87%E5%AD%97%E3%81%A7%E5%8C%BA%E5%88%87%E3%82%89%E3%82%8C%E3%81%9F%E5%80%A4%E3%81%AE%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%20Emacs%20%E3%81%A7%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%99%E3%81%8B%3F.png)
読みやすいように強調表示しながら、区切り文字で区切られた値のファイルを Emacs で表示するにはどうすればよいですか?
理想的には、区切り文字/文字列はカスタマイズ可能である必要があります。
Emacs でできない場合、このタスクを実行するために Linux で使用できる他のツールはありますか?
答え1
@Ammar の解決策に関して、区切り文字として正規表現を使用するようにコマンドを「修正」するのは難しくありませんorg-table-convert-region
。この場合、区切り文字は だけです%%
。1 行追加しました。
(defun org-table-convert-region (beg0 end0 &optional separator)
"Convert region to a table.
The region goes from BEG0 to END0, but these borders will be moved
slightly, to make sure a beginning of line in the first line is included.
SEPARATOR specifies the field separator in the lines. It can have the
following values:
'(4) Use the comma as a field separator
'(16) Use a TAB as field separator
integer When a number, use that many spaces as field separator
nil When nil, the command tries to be smart and figure out the
separator in the following way:
- when each line contains a TAB, assume TAB-separated material
- when each line contains a comma, assume CSV material
- else, assume one or more SPACE characters as separator."
(interactive "rP")
(let* ((beg (min beg0 end0))
(end (max beg0 end0))
re)
(goto-char beg)
(beginning-of-line 1)
(setq beg (move-marker (make-marker) (point)))
(goto-char end)
(if (bolp) (backward-char 1) (end-of-line 1))
(setq end (move-marker (make-marker) (point)))
;; Get the right field separator
(unless separator
(goto-char beg)
(setq separator
(cond
((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
((not (re-search-forward "^[^\n,]+$" end t)) '(4))
(t 1))))
(goto-char beg)
(if (equal separator '(4))
(while (< (point) end)
;; parse the csv stuff
(cond
((looking-at "^") (insert "| "))
((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
(replace-match "\\1")
(if (looking-at "\"") (insert "\"")))
((looking-at "[^,\n]+") (goto-char (match-end 0)))
((looking-at "[ \t]*,") (replace-match " | "))
(t (beginning-of-line 2))))
(setq re (cond
((stringp separator) separator) ;; <-- I added this line
((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
((equal separator '(16)) "^\\|\t")
((integerp separator)
(if (< separator 1)
(error "Number of spaces in separator must be >= 1")
(format "^ *\\| *\t *\\| \\{%d,\\}" separator)))
(t (error "This should not happen"))))
(while (re-search-forward re end t)
(replace-match "| " t t)))
(goto-char beg)
(org-table-align)))
残念ながら、これはエスケープされない|
ので非常にイライラしますし、引用符もまったく処理されません。セルに区切り文字が表示されていないと仮定すると、|
他の何か (たとえば、\vert{}
LaTeX にエクスポートする場合、または Unicode 文字 である ⏐ VERTICAL LINE EXTENSION
) に置き換えて、 の修正バージョンを実行する関数を記述するのは難しくないはずです。必要に応じて、 とを にorg-table-convert-region
置き換えることもできます。もちろん、必要な区切り文字の代わりとして を使用しました(これは関数の引数になる場合があります)。"%%
%%"
%%
%%
それは、そのようなファイルをどのくらいの頻度で見るか、それにどのくらいの作業を入れたいかを知るために必要な機能によって決まります。 :-)
答え2
highlight-phrase
emacsでは、 (M-s h p
)またはhighlight-regexp
( )を使用してテキストを強調表示できますM-s h r
。
答え3
区切り文字を | に変更し (たとえば にsed
変更しますが、まずすべての | を別のものに置き換えます)、すべての行頭と行末に 1 つの | を追加してから、 の emacs でファイルを開きますorg-mode
。
csv-mode
およびその を使用することもできますcsv-align-fields
。
答え4
org-mode をお持ちの場合は、CSV ファイルを開き、メジャー モードを org-mode に設定し、バッファ全体をマークして をクリックし、C-|
CSV ファイルを org-mode テーブルに変換します。
org-modeテーブルに対して何でもできます。emacsのcalcと組み合わせると、スプレッドシートアプリケーションよりも強力になります。ここ参考のため。
Linux の場合、CSV ファイルの処理に使用できるツールは無数にありますが、その中でもスイスナイフは間違いなく awk です。可能であれば awk を学んでください。そうすれば、作業が楽になります。