Jay*_*cee 6 linux newline sed append
我正在尝试做我的作业,仅限于使用sed过滤输入文件到某种格式的输出.这是输入文件(命名stocks):
Symbol;Name;Volume
================================================
BAC;Bank of America Corporation Com;238,059,612
CSCO;Cisco Systems, Inc.;28,159,455
INTC;Intel Corporation;22,501,784
MSFT;Microsoft Corporation;23,363,118
VZ;Verizon Communications Inc. Com;5,744,385
KO;Coca-Cola Company (The) Common;3,752,569
MMM;3M Company Common Stock;1,660,453
================================================
Run Code Online (Sandbox Code Playgroud)
输出需要是:
BAC, CSCO, INTC, MSFT, VZ, KO, MMM
Run Code Online (Sandbox Code Playgroud)
我确实提出了一个解决方案,但效率不高.这是我的sed脚本(命名try.sed):
/.*;.*;[0-9].*/ { N
N
N
N
N
N
s/\(.*\);.*;.*\n\(.*\);.*;.*\n\(.*\);.*;.*\n\(.*\);.*;.*\n\(.*\);.*;.*\n\(.*\);.*;.*\n\(.*\);.*;.*/\1, \2, \3, \4, \5, \6, \7/gp
}
Run Code Online (Sandbox Code Playgroud)
我在shell上运行的命令是:
$ sed -nf try.sed stocks
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有更好的方法使用sed来获得相同的结果?我编写的脚本只能使用7行数据.如果数据较长,我需要重新修改我的脚本.我不知道如何才能让它变得更好,所以我在这里寻求帮助!
谢谢你的任何建议.
另一种使用方法sed:
sed -ne '/^====/,/^====/ { /;/ { s/;.*$// ; H } }; $ { g ; s/\n// ; s/\n/, /g ; p }' stocks
Run Code Online (Sandbox Code Playgroud)
输出:
BAC, CSCO, INTC, MSFT, VZ, KO, MMM
Run Code Online (Sandbox Code Playgroud)
解释:
-ne # Process each input line without printing and execute next commands...
/^====/,/^====/ # For all lines between these...
{
/;/ # If line has a semicolon...
{
s/;.*$// # Remove characters from first semicolon until end of line.
H # Append content to 'hold space'.
}
};
$ # In last input line...
{
g # Copy content of 'hold space' to 'pattern space' to work with it.
s/\n// # Remove first newline character.
s/\n/, /g # substitute the rest with output separator, comma in this case.
p # Print to output.
Run Code Online (Sandbox Code Playgroud)