加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

linux – 如何编写sed脚本来从文本文件中grep信息

发布时间:2021-01-08 14:59:29 所属栏目:Linux 来源:网络整理
导读:我正在尝试做我的作业,仅限于使用sed将输入文件过滤到某种格式的输出.这是输入文件(名为stocks): Symbol;Name;Volume================================================BAC;Bank of America Corporation Com;238,059,612CSCO;Cisco Systems,Inc.;28,159,4

我正在尝试做我的作业,仅限于使用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

================================================

输出需要是:

BAC,CSCO,INTC,MSFT,VZ,KO,MMM

我确实提出了一个解决方案,但效率不高.这是我的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
}

我在shell上运行的命令是:

$sed -nf try.sed stocks

我的问题是,有没有更好的方法使用sed来获得相同的结果?我编写的脚本只能使用7行数据.如果数据较长,我需要重新修改我的脚本.我不知道如何才能让它变得更好,所以我在这里寻求帮助!

谢谢你的任何建议.

最佳答案 使用sed的另一种方法:

sed -ne '/^====/,/^====/ { /;/ { s/;.*$// ; H } }; ${ g ; s/n// ; s/n/,/g ; p }' stocks

输出:

BAC,MMM

说明:

-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.

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读