sed 将括号放在开头并在第 4 行结束 [英] sed place parentheses at the beginning and close on the 4th line

查看:37
本文介绍了sed 将括号放在开头并在第 4 行结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在第一行放置一个左括号并将其关闭作为第四行的结尾.下面是我要查找的数据后跟输出的示例.

Im trying to place a open parenthesis on the first line and close it as the end of the 4th line. Below is a example of the data followed by the output that I am looking for.

tester1
SERVICE_TICKET_CREATED
Thu Mar 19 23:27:57 UTC 2015
192.168.1.3
tester2
SERVICE_TICKET_CREATED
Fri Mar 20 00:31:59 UTC 2015
192.168.1.2

(tester1
SERVICE_TICKET_CREATED
Thu Mar 19 23:27:57 UTC 2015
192.168.1.3)
(tester2
SERVICE_TICKET_CREATED
Fri Mar 20 00:31:59 UTC 2015
192.168.1.2)

推荐答案

使用 awk 你可以这样做

awk 'NR%4==1{print "("$0; next} NR%4==0{print $0")"; next}1'

测试

$ awk 'NR%4==1{print "("$0; next} NR%4==0{print $0")"; next}1' input
(tester1
SERVICE_TICKET_CREATED
Thu Mar 19 23:27:57 UTC 2015
192.168.1.3)
(tester2
SERVICE_TICKET_CREATED
Fri Mar 20 00:31:59 UTC 2015
192.168.1.2)

较短的版本

awk 'NR%4==1{$0="("$0} NR%4==0{$0=$0")"}1'

这篇关于sed 将括号放在开头并在第 4 行结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆