Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux colrm 命令詳解:從文件中移除指定欄位

編輯:Linux 系統常見問題解答

命令簡介

colrm(Column Remove)用於從標準輸入讀取文本,移除指定欄位範圍後輸出到標準輸出。常用於管道處理固定寬度欄位的文本數據。

語法格式

colrm [start_col [end_col]]
  • start_col:起始欄位(從 1 計數),單獨提供時表示從該欄位刪除到行尾。
  • end_col:結束欄位(可選),提供時表示刪除 start_col 到 end_col 之間的所有字符。

實戰範例

假設文件 test.txt 內容:

123456789

範例 1:刪除第 4 欄之後所有字符

$ colrm 4 < test.txt
123

範例 2:刪除第 4 到第 6 欄

$ colrm 4 6 < test.txt
123789

範例 3:輸出重導向保存結果

$ colrm 4 6 < test.txt > test.txt.new
$ cat test.txt.new
123789

使用場景

  • 處理固定寬度日誌/報表,快速剔除不需要的欄位。
  • 配合 cut、awk 進行更複雜的欄位提取與重組。
  • 腳本中快速截取字串特定區段。

注意事項

  • 欄位以字符(位元組)為單位,非字段分隔符模式(如需按分隔符處理請用 cut -d 或 awk)。
  • 輸入需為標準輸入,不直接接受文件名參數。
  • 多位元組字符(中文等)環境下,按字節計算可能導致截斷亂碼,建議配合 awk 處理。

總結

colrm 是輕量級的欄位刪除工具,適合簡單固定寬度文本處理。複雜場景下,awk、cut、sed 提供更強大的欄位操作能力。

Copyright © Windows教程網 All Rights Reserved