简体   繁体   English

使用 sed 时列未对齐 \t

[英]Columns not aligning when using sed \t

I have an input file named rectangle.txt and my aim is to re-format the contents and output it to rectangle_f.txt using only sed commands.我有一个名为 rectangle.txt 的输入文件,我的目标是仅使用 sed 命令将内容和 output 重新格式化为 rectangle_f.txt。

I have managed to format it the content from:我设法将其格式化为以下内容:

Name,Length,Width,Area,Owner
Rec1,9,9,81,Em
Rec2,2,2,4,Soph
(etc...)

To look like this:看起来像这样:

Name: Rec1    Length: 9    Width: 9    Area: 81    Owner: Em
Name: Rec2    Length: 2    Width: 2    Area: 4     Owner: Soph

My issue is when it comes to names with double digits (example: Sq10) the tab that I have inserted between Name and Length is one space too long.我的问题是当涉及到带有两位数的名称(例如:Sq10)时,我在名称和长度之间插入的选项卡太长了一个空格。 For example:例如:

Name: Rec9    Length: 6    Width: 6    Area: 36    Owner: Jay
Name: Rec10    Length: 7     Width: 7     Area: 49    Owner: Chris

Does anyone know the best way to make all columns line up?有谁知道使所有列对齐的最佳方法? The code I currently have is:我目前拥有的代码是:

 sed -e "1d" \
        -e 's/^/Name: /;s/,/ \t\tHeight: /;s/,/ \t\tWidth: /;s/,/\t\tArea: /;s/,/ \t\tColour: /' rectangle.txt > rectangle_f.txt  

I need it to apply only to Rec10 to Rec20 (inclusive) which also are code lines 10-20.我需要它仅适用于 Rec10 到 Rec20(包括),它们也是代码行 10-20。 Any help would be great!任何帮助都会很棒!

Have you tried:你有没有尝试过:

sed 1d file.txt | sed -E 's/([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)/Name: \1\tLength: \2\tWidth: \3\tArea: \4\tOwner: \5\t/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM