Format input text file strictly using sed

六眼飞鱼酱① 提交于 2020-06-17 15:45:26

问题


I have this text file named file.txt with the content bellow:

Name,Height,Width,Area,Colour
Rec1,5,10,50,Black
Rec2,8,11,88,Red
Rec3,9,13,117,Red
Rec4,13,16,208,Blue
Rec5,15,17,262,Red
Rec6,17,19,341,Green
Rec7,20,21,430,Black
Rec8,22,23,528,Red
Rec9,25,25,637,Blue
Rec10,27,27,756,Green
Rec11,30,29,885,Black
Rec12,32,31,1023,Blue
Rec13,35,33,1172,Red
Rec14,37,35,1331,Blue
Rec15,40,37,1500,Black
Rec16,42,39,1678,Green
Rec17,45,41,1867,Red
Rec18,47,43,2066,Blue
Rec19,50,45,2275,Black
Rec20,52,47,2493,Red

I need to format it to have to output from the attached picture, using a bash script and pretty much only using linux sed command

What I came up with so far:

sed -n '/Name/{h}; /Rec/{x; s/,/:/g; s/Height.*$//g; p; x; p}' file.txt

回答1:


Try this:

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

Thanks, Stefan



来源:https://stackoverflow.com/questions/61870873/format-input-text-file-strictly-using-sed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!