向文件添加行



我需要有关向文件添加一些行的帮助;对于 expamle,我从文本中 grep 此参数并将其保存到 File1:

cellIdentity="461"
cellIdentity="465"
cellIdentity="462"
cellIdentity="466"
cellIdentity="463"
cellIdentity="467"

现在我需要创建另一个文件,File2,如下所示:

cellIdentity="461"
cellIdentity="465"
 cellIdentity="468"
cellIdentity="462"
cellIdentity="466"
 cellIdentity="469"
cellIdentity="463"
cellIdentity="467"
 cellIdentity="460"

我隔开了三行新行。所以基本上,我需要添加这 3 行,最后一个数字的 8,9,0 在这三行中总是相同的,前两位数字与其他行一样。我不知道是否有可能做到。我正在尝试使用 sed 命令,但没有运气。我在 solaris 中使用/bin/csh。任何帮助/提示都会很棒。

谢谢

使用 awk

awk 'BEGIN{split("8 9 0",a," ")}NR%2==0{t=$0;sub(/.$/,a[++i],$2);$0=t RS $0}1' FS=" OFS=" file
cellIdentity="461"
cellIdentity="465"
cellIdentity="468"
cellIdentity="462"
cellIdentity="466"
cellIdentity="469"
cellIdentity="463"
cellIdentity="467"
cellIdentity="460"

解释

  • FS=" OFS="定义字段分隔符"
  • BEGIN{split("8 9 0",a," ")}定义数组中的 8,9,0 a 在 BEGIN 部分中
  • NR%2==0找到奇数行
  • sub(/.$/,a[++i],$2)替换第 2 列中最后一个字符,该字符一个接一个地从数组a获取
  • 1与打印相同

这可能对你有用(GNU sed):

sed -r '1{x;s/^/890/;x};2~2{p;G;s/(.)("n)(.)(.*)/3243/;P;s/.*n//;h;d}' file

最新更新