开发者

use SED to extract header and data from "x=a1, y=b1, z=c1" type of files

The data file looks like:

x=a1, y=b1, z=c1
x=a2, y=b2, z=c2
...

I want to parse it to a more useab开发者_JAVA百科le format:

x   y   z
a1  b1  c1
a2  b2  c2
...

The header "x,y,z" and the data "a, b, c" does not contain "=" or ",".

using

 1 s/=*[^=]*[,$]/ /g

give me

 x y z=c1

Apparently the last item is not matched with "[,$]" Any suggestions?

Many thanks!

Dong


The [,$] matches either a comma or a dollar, not comma or end of line.

It is probably simplest to do two operations on the first line:

sed -e '1{ s/=[^,]*,//g; s/=.*//; }' ...

The first looks for everything between an equals sign and the first comma (including the delimiters) and deletes it, repeatedly; the second looks for everything after the last (only) equals sign and deletes that.


sed -i 's/[^=]*=\([^,]*\)/\1 /g'

Should be able to get all the data into this format

a1  b1  c1
a2  b2  c2
... 

After that you can insert a header of your choosing with

sed -i 1i"header"

If you want you can also parse the header out of the file using the sed in Jonathan Leffler's answer.


To parse the file to CSV,

 s/=[^,]*//g

 s/[^,]*=//g

give header and data, respectively.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜