如何从输出中删除前两行(bash)

26

我在一个文本文件中有以下输出:

106 pages in list
.bookmarks
20130516 - Daily Meeting Minutes
20130517 - Daily Meeting Minutes
20130520 - Daily Meeting Minutes
20130521 - Daily Meeting Minutes

我希望删除输出的前两行。我使用的这个shell脚本总是有这前两行。

这是我生成和读取文件的方式:

#Lists
PGLIST="$STAGE/pglist.lst";
RUNSCRIPT="$STAGE/runPagesToMove.sh";

#Get List of pages
$ATL_BASE/confluence.sh $CMD_PGLIST $CMD_SPACE "$1" > "$PGLIST";

# BUILD executeable script
echo "#!/bin/bash" >> $RUNSCRIPT 2>&1
IFS=''
while read line
  do
     echo "$ATL_BASE/conflunce.sh $CMD_MVPAGE $CMD_SPACE "$1" --title \"$line\" --newSpace \"$2\" --parent \"$3\"" >> $RUNSCRIPT 2>&1
done < $PGLIST

如何删除那两条顶部的线?


这些行看起来像是在你的其他脚本中生成的。尝试在其他文件(如runPagesToMove.shconfluence.sh)中查找echo语句。如果您发布这些文件,有人可能能够解决问题,但是从给定的代码来看,问题实际上是无法解决的。 - Adam
3个回答

40

使用tail命令可以实现此目的:

tail -n +3 "$PGLIST"
  -n, --lines=K
          output the last K lines, instead of the last 10; or use -n +K
          to output starting with the Kth

哇,甚至在macOS上也能运行!macOS在其“man”页面中没有提到这一点。 - Patrick

34
经典的解决方案是使用sed删除第1和第2行:
sed 1,2d "$PGLIST"

昨晚我已经让这个工作了 - ./runscript.sh > "$PGLIST" | sed 1,2d $PGLIST,但出于某种原因,现在它不起作用了。所以,我也尝试了另一种方式,但没有成功。./runscript.sh > "$PGLIST" | sed 1,2d $PGLIST > $PGLIST; - noober
昨晚你展示的东西并没有“运行”。昨晚,你要么省略了 >,要么在 | 的位置上放了一个分号(或换行符)。小心尝试在管道中读写单个文件;通常会在读取之前覆盖该文件,这会导致沮丧。总的来说,我认为你要找的是 ./runscript.sh | sed 1,2d > "$PGLIST",但我不能确定。 - Jonathan Leffler

9

awk方式:

awk 'NR>2' "$PGLIST"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接