I am trying to do my homework, which is limited only to using sed
to filter the input file in a specific output format. Here is the input file (named 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 ================================================
And the output should be:
BAC, CSCO, INTC, MSFT, VZ, KO, MMM
I came up with a solution, but it is inefficient. Here is my sed
script (named 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 }
The command that I run on the shell:
$ sed -nf try.sed stocks
My question is: is there a better way to use sed to get the same result? script I wrote only works with 7 rows of data. If the data is longer, I need to re-edit my script. I'm not sure how I can do this better, so I ask for help!
Thanks for any recommendations.