首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何按照模式将文本块和下一行移动到文件末尾?

如何按照模式将文本块和下一行移动到文件末尾?
EN

Stack Overflow用户
提问于 2022-09-14 02:16:54
回答 5查看 118关注 0票数 3

我有这个ssh配置,需要编辑。

代码语言:javascript
复制
Host vps6
   HostName 123.456.789.00
   User dylan
   Port 123

Host vps4
   HostName 123.456.789.00 
   User dylan
   Port 123

# old server

Host vps3-old
   HostName 123.456.789.00
   User dylan 
   Port 123

我希望将vps6的配置移到文件的末尾,并将-old附加到它的配置别名。生成的文件将是。

代码语言:javascript
复制
Host vps4
   HostName 123.456.789.00 
   User dylan
   Port 123

# old server

Host vps3-old
   HostName 123.456.789.00
   User dylan 
   Port 123

Host vps6-old
   HostName 123.456.789.00
   User dylan
   Port 123

我使用sed命令sed '/'"vps4"'/{N;N;N;N;H;$!d}; ${p;x;s/'"vps4"'/'"vps4"'-old/}'成功地做到了这一点,不幸的是,这在文件末尾给了我不想要的换行符。

代码语言:javascript
复制
[tmp]$ sed '/'"vps6"'/{N;N;N;N;H;$!d}; ${p;x;s/'"vps6"'/'"vps6"'-old/}' config
Host vps4
   HostName 123.456.789.00
   User dylan
   Port 123

# old server

Host vps3-old
   HostName 123.456.789.00
   User dylan 
   Port 123

Host vps6-old
   HostName 123.456.789.00 
   User dylan
   Port 123

[tmp]$ # See above me

此外,我希望能够指定要移动的下一个n行(例如,上面将标记Host vps4,接下来的3行将移动到文件末尾)。我搜索了这个网络,发现这类任务推荐的工具是ed,但是我还没有找到示例命令来做我想做的事情。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2022-09-14 02:49:47

在显示的示例中,请尝试遵循awk代码。

代码语言:javascript
复制
awk '
!NF && found{
  found=""
  next
}
/^Host vps6/{
  found=1
  line=$0"-old"
  next
}
found{
  val=(val?val ORS:"")$0
  next
}
!found
END{
  print ORS line ORS val
}
'  Input_file

注意:如果您想将输出保存到Input_file本身,然后运行上面的程序,它将在终端上打印输出,一旦您对上述程序的结果感到满意,就可以将> temp && mv temp Input_file附加到上面的程序中,以执行内部保存到Input_file中。

说明:添加了对上述用过的代码的详细说明。

代码语言:javascript
复制
awk '                      ##Starting awk program from here.
!NF && found{              ##Checking if line is empty AND found is SET then do following.
  found=""                 ##Nullifying found here.
  next                     ##next will skip all further statements from here.
}
/^Host vps6/{              ##If line starts from Host vps6 then do following.
  found=1                  ##Setting found here.
  line=$0"-old"
  next
}
found{                     ##If found is set then do following.
  val=(val?val ORS:"") $0  ##Creating val which is keep adding current line into it.
  next                     ##next will skip all further statements from here.
}
!found                     ##If found is NOT set then print that line.
END{                       ##Starting END block of this program from here.
  print ORS line ORS val   ##Printing ORS line ORS and val here.
}
'  Input_file              ##Mentioning Input_file name here. 
票数 5
EN

Stack Overflow用户

发布于 2022-09-14 04:17:28

另一只鹰:

代码语言:javascript
复制
$ awk -v RS= '{                   # read blank line separated records
    if(/^Host vps6/) {            # buffer matching record
        sub(/vps6/,"vps6-old")    # add -old
        b=$0
    } else                        # print non-matching
        print $0 ORS              # with with extra newline
}
END {                             # in the end
    print b                       # output buffer
}' file

如果您想使用变量而不是硬编码字符串,请使用以下内容:

代码语言:javascript
复制
$ awk -v s=vps6 -v RS= '{         # or -v s="$sshname"
    if($0~"^Host " s) {           
        match($0,s)
        b=substr($0,1,RSTART+RLENGTH-1) "-old" substr($0,RSTART+RLENGTH)
    } else
        print $0 ORS
}
END {
    print b
}
票数 3
EN

Stack Overflow用户

发布于 2022-09-14 03:08:35

编辑:这与@RavinderSingh13的回答基本相同,但没有那么好。

另一个可能的选择是:

代码语言:javascript
复制
cat file1
Host vps6
   HostName 123.456.789.00
   User dylan
   Port 123

Host vps4
   HostName 123.456.789.00
   User dylan
   Port 123

# old server

Host vps3-old
   HostName 123.456.789.00
   User dylan
   Port 123
代码语言:javascript
复制
awk '
{flag=0}
/vps6/,/^$/ {flag=1}
{
    gsub("vps6", "vps6-old")
    if (flag == 0) print
    if (flag == 1) a[NR]=$0
}
END {
    print ""
    for (i in a) {
        if(a[i] != "") print a[i]
    }
}' file1

Host vps4
   HostName 123.456.789.00
   User dylan
   Port 123

# old server

Host vps3-old
   HostName 123.456.789.00
   User dylan
   Port 123

Host vps6-old
   HostName 123.456.789.00
   User dylan
   Port 123
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73710773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档