【发布时间】:2015-04-23 08:35:02
【问题描述】:
我正在尝试编写一个脚本,将“测试”文件中的每一行保存在变量 line1、line2 等中......
x=$(cat test | wc -l) #here i have how many lines
i="1"
while [ "$i" -lt "$x" ]
do
line$i=$(sed '$iq;d' test) #i try to get the number $i line one by one
i=$[$i+1]
done
你能帮帮我吗?
谢谢!
【问题讨论】:
-
在您运行它的目录中是否有一个名为
test的文件? -
是的,当我输入纳米测试时:2015-01-29/ 2015-01-30/ 2015-01-31/ 2015-02-01/ 2015-02-02/ 2015-02- 03/ 2015-02-04/ 2015-02-05/ 2015-02-06/
-
line$1很奇怪。尝试重命名此变量。 -
我可以帮助你:
mapfile -t lines < test。您将拥有一个漂亮的数组lines,其中的字段包含文件的行。打印第三行:printf '%s\n' "${lines[2]}"(是的,赋值从字段 0 开始,但可以使用-O选项更改)。 -
$[$i+1]表示法已过时;请改用$(($i+1))。
标签: bash variables sed while-loop variable-assignment