BASH:变量仅显示最后一行

时间:2011-07-26 13:51:34

标签: linux bash

该变量仅显示最后一行:

while read var1; do
    var2+="$var1"$"\n"
    if [[ $var2 == *andoi* ]]; then
        break
    fi
done < "./filename" #or other file
echo "Result:"
echo "$var2" #just read the last lines, I want all
#...edit $var2...etc...
echo "$var2" #modified

简化脚本:

while read var1; do
    var2+="$var1"$"\n"
done < "./filename" #or other file
echo "$var2" #just read the last lines, I want all
#...edit $var2...cdn...
echo "$var2" #modified

我使用了这个http://gsteph.blogspot.com/2007/04/bash-reading-text-file.html

牙齿变量如何显示一个变量中的所有行?

//我纠正了,这可能对某人有用.//

2 个答案:

答案 0 :(得分:2)

所以你想将$var1积累到$var2

while read var1; do
  var2="$var2 $var1"
  # etc...
done < "./filename"

echo "$var2"

要使用换行符累加它们,请执行以下操作:

while read var1; do
  var2="$var2
$var1"
  # etc...
done < "./filename"

echo "$var2"

答案 1 :(得分:0)

如果您只想要一个文件的内容,包括一些关键字:

x=$(sed '/droid/ q')
x=$(awk '{print} /droid/ {exit}')