簡體   English   中英

在Linux中將分隔文件轉換為固定寬度

[英]Convert delimited file to fixed width in Linux

使用可用於bash的工具,如何轉換分隔數據

foo|bbbaaarrr|bazz

固定寬度數據

foo      bbbaaarrrbazz     EOL   

我嘗試使用列作為文檔隱含我可以定義列寬,沒有用。 我確定使用sed或awk這是微不足道的,但我不熟悉它們。

以下內容對您有用:

column -t -s '|' input_file_here

它會將輸入文件轉換為表格格式。 輸入記錄分隔符由-s指定。 如果在字段中需要除空格填充之外的其他內容,請使用-o設置輸出分隔符。 輸出分隔符默認為兩個空格,因此輸出中每列之間將有兩個空格。

例:

$ cat input
hello|world|testing
a|b|c
another|test|line

$ column -t -s '|' input
hello    world  testing
a        b      c
another  test   line

http://man7.org/linux/man-pages/man1/column.1.html


編輯:

如果您需要每個字段都是固定長度,則可以使用awk 您需要為文件設置輸入分隔符,但是這樣的東西應該有效:

$ awk -F '|' '{ for (i=1; i<=NF; i++) { printf("%-10s", $i); } print ""; }' input
hello     world     testing
a         b         c
another   test      line

只需更改printf語句中指定的字段寬度即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM