0

我有一个文件consC,它是一行空格和 *,看起来像这样:

                                          **** **     *          

如何读入一个保持*位置完整而不丢失空格的字符串,然后获取*的索引

4

2 回答 2

2
echo '          **  **   *****    *     ' > consC.txt
consC="$(cat consC.txt)"
echo "$consC"

Edit: One of the comments mentions that the second line can be simplified:

consC=$(< consC.txt)
  • No need to use cat as < will do the job,
  • and double-quotes not needed when using $(...) construct in an assignment

Although double-quotes are definitely needed in line 3: echo.

于 2013-08-26T01:07:29.827 回答
1

您还可以设置输入字段分隔符:

while IFS= read line; do
    echo "$line"
done < input
于 2013-08-26T01:52:39.927 回答