我正在努力解决这个问题。
$ cat test.txt
server1
ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
Search
ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web
ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web
ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2
ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb
ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
ec2dd
ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2
ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb
ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
我需要这样的输出:
$ cat test.txt
server1:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
Search:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
Web:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
Web:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
server2:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
loaddb:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
ec2dd:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
server2:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
loaddb:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
答案 0 :(得分:4)
sed
:$ sed 'N;s/\n/:/' test.txt
server1:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
Search:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2:ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
ec2dd:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2:ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
此方法使用两个sed
命令:
N
从文件中读取第二行并将其附加到模式空间。这样,模式空间总是有两条连续的行。
s/\n/:/
从模式空间中的两行之间删除换行符,并用冒号替换它。
awk
:$ awk 'NR%2==1{name=$1;next} {print name ":" $0;}' test.txt
server1:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
Search:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2:ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
ec2dd:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2:ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
注意:
NR%2==1{name=$1;next}
这将读取第一个和所有后续的奇数 - 编号行,并将第一个字段分配给变量name
。然后跳过其余的命令并跳转到next
行重新开始。
print name ":" $0
在偶数行上,打印名称,冒号和当前行。
while read name; read line
do
printf "%s:%s\n" "$name" "$line"
done <test.txt
此处,一行从text.txt
读入变量name
,下一行读入变量line
。然后在它们之间打印这两个。
答案 1 :(得分:2)
您只需使用paste
命令
paste -d: - - < file
通过Perl,
perl -pe 's/\n/:/g if $.%2==1' file
Perl中的 $.
与awk中的NR
类似。所以它只需要奇数行,并且仅在特定行上用:
替换换行符。
答案 2 :(得分:2)
此awk
可能会这样做,但如果您不完全理解,请小心getline
。
awk '{a=$1;getline;print a":"$1}' file
server1:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
Search:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2:ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
ec2dd:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2:ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
这是一种更好的方法:
awk 'ORS=NR%2?":":RS' file
server1:ec2-xx.xx.xx.xxus-west-2.compute.amazonaws.com
Search:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
Web:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2:ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
ec2dd:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
server2:ec2-xx.xx-xx-xx.us-west-2.compute.amazonaws.com
loaddb:ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com