preserving前导空格,而读>>通过在bash行写入文件行 [英] Preserving leading white space while reading>>writing a file line by line in bash

查看:127
本文介绍了preserving前导空格,而读>>通过在bash行写入文件行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过文本文件的目录试图环和它们合并成一个文档。这个伟大的工程,但文本文件包含code段,和我所有的格式是越来越晕倒在左边。线路上的所有领先的空白被剥离。

I am trying to loop through a directory of text files and combine them into one document. This works great, but the text files contain code snippets, and all of my formatting is getting collapsed to the left. All leading whitespace on a line is stripped.

#!/bin/sh
OUTPUT="../best_practices.textile"
FILES="../best-practices/*.textile"
for f in "$FILES"
do
  echo "Processing $f file..."
  echo "">$OUTPUT

  cat $f | while read line; do 
      echo "$line">>$OUTPUT
  done
  echo >>$OUTPUT
  echo >>$OUTPUT
done

我承认一个bash小白,但寻找高和低后,我无法找到一个妥善的解决办法。显然BASH讨厌一般领先的空白。

I am admittedly a bash noob, but after searching high and low I couldn't find a proper solution. Apparently BASH hates the leading white space in general.

推荐答案

而不是:

cat $f | while read line; do 
    echo "$line">>$OUTPUT
done

做到这一点:

cat $f >>$OUTPUT

(如果您需要按行做的事情行它会是不错的,包括在问题的原因。)

(If there's a reason you need to do things line by line it'd be good to include that in the question.)

这篇关于preserving前导空格,而读>>通过在bash行写入文件行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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