我如何preserve领先的回声空格的shell脚本? [英] How do I preserve leading whitespaces with echo on a shell script?
问题描述
我有一个源文件是已经合并在一起的多个文件的组合。我的脚本应该将它们分开成原来的单个文件。
每当遇到以FILENM开始的行,这意味着它的下一个文件的开头。
在所有文件的详细线路是固定的宽度;所以,我目前遇到在那里,与领先的空格开始的行被截断时,它不应该被截断的问题。
我如何提高这个剧本保留了领先的空格?
而读线
做
线型=`回声$线|切-c1-6`
如果[$线型==FILENM];然后
文件名=`回声$线|切-C7-`
其他
回声$行>> $文件路径/文件名$
科幻
完成< $文件路径/的资源文件
的前导空格,因为删除读
拆分输入单词。为了解决这个问题,将 IFS
变量空字符串。像这样的:
OLD_IFS =$ IFS
IFS =
而读线
做
...
完成< $文件路径/的资源文件
IFS =$ OLD_IFS
I have a source file that is a combination of multiple files that have been merged together. My script is supposed to separate them into the original individual files.
Whenever I encounter a line that starts with "FILENM", that means that it's the start of the next file.
All of the detail lines in the files are fixed width; so, I'm currently encountering a problem where a line that starts with leading whitespaces is truncated when it's not supposed to be truncated.
How do I enhance this script to retain the leading whitespaces?
while read line
do
lineType=`echo $line | cut -c1-6`
if [ "$lineType" == "FILENM" ]; then
fileName=`echo $line | cut -c7-`
else
echo "$line" >> $filePath/$fileName
fi
done <$filePath/sourcefile
The leading spaces are removed because read
splits the input into words. To counter this, set the IFS
variable to empty string. Like this:
OLD_IFS="$IFS"
IFS=
while read line
do
...
done <$filePath/sourcefile
IFS="$OLD_IFS"
这篇关于我如何preserve领先的回声空格的shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!