深入学习ing

shell将2个文件的前两行和最后一行中间内容合并的编写过程

  1-初步想法:将固定的头和尾写死追加到内容前后:

echo -e '<?xml version="1.0"?>\n<doc>' >> c.txt \
&& cat a.txt | sed '1,2d' | sed '$d' >> c.txt \
&& cat b.txt | sed '1,2d' | sed '$d' >> c.txt \
&& echo '</doc>' >> c.txt

 

  2-改进用法:

head -2 a.txt >> c.txt \
&& sed '1,2d' a.txt | sed '$d' >> c.txt \
&& sed '1,2d' b.txt | sed '$d' >> c.txt \
&& tail -n 1 a.txt >> c.txt

  重点命令: head、sed、tail。     符号: ">>"、"&&"、 "|" 、 "\"

 

附加:

  a.txt和b.txt文件:

<!html>
<html>
  <body>
      aaaa
  </body>
</html>

  合并文件:

<!html>
<html>
  <body>
      aaaa
  </body>
  <body>
      bbb
  </body>
</html>

 

posted on 2021-09-03 17:57  深入学习ing  阅读(243)  评论(0编辑  收藏  举报

导航