我该如何组合这些线条

时间:2014-04-15 15:01:14

标签: bash shell debian

如何组合这些行(注意:这只是一个更大文件的摘录):

interface GigabitEthernet0/0
 no ip proxy-arp
interface GigabitEthernet0/0.202
 no ip proxy-arp
interface GigabitEthernet0/0.358
 no ip proxy-arp
interface FastEthernet0/0/1.26
 no ip proxy-arp
interface GigabitEthernet0/0
 no ip proxy-arp
interface GigabitEthernet0/0.202
 no ip proxy-arp
interface GigabitEthernet0/0.358
 no ip proxy-arp
interface FastEthernet0/0/1.26
 no ip proxy-arp
interface Loopback0
 no ip proxy-arp
interface Loopback1
 no ip proxy-arp
interface Loopback26
 no ip proxy-arp
interface GigabitEthernet0/0/0
 no ip proxy-arp

这样他们就像这样:

interface GigabitEthernet0/0 | no ip proxy-arp
interface GigabitEthernet0/0.202 | no ip proxy-arp
interface GigabitEthernet0/0.358 | no ip proxy-arp
interface FastEthernet0/0/1.26 | no ip proxy-arp
interface GigabitEthernet0/0 | no ip proxy-arp
interface GigabitEthernet0/0.202 | no ip proxy-arp
interface GigabitEthernet0/0.358 | no ip proxy-arp
interface FastEthernet0/0/1.26 | no ip proxy-arp
interface Loopback0 | no ip proxy-arp
interface Loopback1 | no ip proxy-arp
interface Loopback26 | no ip proxy-arp
interface GigabitEthernet0/0/0 | no ip proxy-arp

我尝试了一些我在网上找到的不同解决方案,但似乎没有一种解决方案适用于我的情况

4 个答案:

答案 0 :(得分:4)

粘贴可以做到:

paste -d '|' - - < file

答案 1 :(得分:3)

试试这个sed one-liner:

sed 'N;s/\n/ | /;s/\r//g' file.txt

答案 2 :(得分:2)

 awk '{printf "%s%s",$0,(NR%2?"|":"\n")}' file

答案 3 :(得分:0)

while IFS= read line1 && IFS= read line2; do
    printf "%s | %s\n" "$line1" "$line2"
done < file.txt