如何在 Emacs 中查看分隔符號分隔值檔案並突出顯示以利於閱讀?

如何在 Emacs 中查看分隔符號分隔值檔案並突出顯示以利於閱讀?

如何在 Emacs 中查看分隔符號分隔值檔案並突出顯示以利於閱讀?

理想情況下,分隔符號/字串應該是可自訂的。

如果 Emacs 無法完成此任務,Linux 上還有其他可用的工具嗎?

答案1

org-table-convert-region關於@Ammar的解決方案“修復”命令以採用正則表達式作為分隔符並不困難,在本例中可能只是%%.我加了一行。

(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。如果您願意,甚至可以替換"%%and 。當然,我已經用作您想要的任何分隔符的替代(這可能是函數的參數)。%%"%%%%

這完全取決於您查看此類文件的頻率以及您需要什麼功能來了解您想要在其中投入多少工作。 :-)

答案2

在 emacs 中,您可以使用highlight-phrase( M-s h p) 或highlight-regexp( M-s h r) 來反白顯示某些文字。

答案3

您可以將分隔符號變更為 | (例如,通過sed,但首先將所有 | 替換為其他內容),添加一個 |到每一行的開頭和結尾,然後在 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,它會讓你的生活更輕鬆。

相關內容