当前位置:网站首页>The upper and lower lines of the shell are merged into one line

The upper and lower lines of the shell are merged into one line

2022-06-25 07:07:00 mixboot

List of articles

shell



Preface

sed


One 、shell Two lines up and down

1.file

  • cat file.txt
host1
192.168.0.1
host2
192.168.0.2
host3
192.168.0.3
host4
192.168.0.4
host5
192.168.0.5

2.sed

  • sed 'N;s/\n/ :/' file
# cat file.txt | sed 'N;s/\n/ :/'
host1 :192.168.0.1
host2 :192.168.0.2
host3 :192.168.0.3
host4 :192.168.0.4
host5 :192.168.0.5

Two 、sed Multiple lines

  • N (next) Multiline operation command , The two lines before and after the text are merged into one line
# cat file.txt
host11
192.168.0.1
host222
192.168.0.2
host3
192.168.0.3
host44
192.168.0.4
host555
192.168.0.5
# sed 'N;s/\n/ :/' file.txt
host11 :192.168.0.1
host222 :192.168.0.2
host3 :192.168.0.3
host44 :192.168.0.4
host555 :192.168.0.5
  • Use “ tabs ”\t Align edges
# sed 'N;s/\n/   \t/' file.txt
host11   	192.168.0.1
host222   	192.168.0.2
host3   	192.168.0.3
host44   	192.168.0.4
host555   	192.168.0.5

Reference resources

  1. shell The two lines merge into one line
  2. sed Multiline command
  3. sed s Replace script command
  4. Use “ tabs ” Align edges
原网站

版权声明
本文为[mixboot]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250447132118.html