使用tac
+awk
解决方案,请您尝试以下操作。只需将line
变量设置awk
为行(从底部),无论您想跳过哪个。
tac Input_file | awk -v line="3" 'line==FNR{next} 1' | tac
说明:使用tac
将反向读取 Input_file(从底行到第一行),将其输出传递给awk
命令,然后检查条件是否行等于行(我们想跳过)然后不打印该行,1 将打印其他线路。
第二种解决方案:使用awk
+wc
解决方案,请尝试以下。
awk -v lines="$(wc -l < Input_file)" -v skipLine="3" 'FNR!=(lines-skipLine+1)' Input_file
说明:在此处启动awk
程序并创建一个变量lines
,其中包含 Input_file 中存在的总行数。变量skipLine
具有我们想要从 Input_file 底部跳过的行号。然后在主程序检查条件下,如果当前行不等于lines-skipLine+1
然后打印这些行。
第三种解决方案:根据 Ed sir 的评论添加解决方案。
awk -v line=3 '{a[NR]=$0} END{for (i=1;i<=NR;i++) if (i != (NR-line)) print a[i]}' Input_file
说明:为第 3 个解决方案添加详细说明。
awk -v line=3 ' ##Starting awk program from here, setting awk variable line to 3(line which OP wants to skip from bottom)
{
a[NR]=$0 ##Creating array a with index of NR and value is current line.
}
END{ ##Starting END block of this program from here.
for(i=1;i<=NR;i++){ ##Starting for loop till value of NR here.
if(i != (NR-line)){ ##Checking condition if i is NOT equal to NR-line then do following.
print a[i] ##Printing a with index i here.
}
}
}
' Input_file ##Mentioning Input_file name here.