我希望能够打印从bash的行数,如:行号(从顶部算起) – >文件的结尾
看来尾巴只会从底部算起。
有谁知道如何做到这一点? 谢谢。
我试过以下
# Where $1 is the file I'm reading in # Get line number of error: LINENUM=$( grep -n "$LAST_ERROR_DATE" $1 | egrep $LOG_THRESHOLD | grep $LAST_HOUR: | sed 's/:/ /g' | awk '{print $1}' | head -n 1 ) echo $LINENUM # This returns 995 # Print everything from linenumber downwards MESSAGE=$( awk 'NR >= $LINENUM' $1 )
这个工程当我手动popup在995 awk而不是$ LINENUM,它似乎没有读取我的bashvariables。 有任何想法吗?
单引号''
意思是,使用这个确切的string。“ 他们不会用variables名称replacevariables名称。 所以这就是为什么你必须手动写入995
。 这有帮助吗?
使用tail -n +X filename
从第tail -n +X filename
行打印到文件结尾。
使用sed -n 'N,$p' filename
从第N行打印到文件末尾。
womble的答案是最好的,但要解决你的问题,最后一行的variables,你可以使用awk的variables传递特性:
MESSAGE=$( awk -v linenum=$LINENUM 'NR >= linenum' $1 )
备用:
line=10 file=/path/to/file sed -n $line,\$p $file