sed line range, all but the last line

前端 未结 5 1046
小蘑菇
小蘑菇 2020-12-08 04:48

You can specify a range of lines to operate on. For example, to operate on all lines, (which is of course the default):

sed -e \"1,$ s/a/b/\"
5条回答
  •  猫巷女王i
    2020-12-08 05:16

    In a more general sense, this issue requires you to edit a stream by specifying a range with one of the range limits being an offset from the end-of-file. The following example shows how to do this. In this example I am printing out all the lines of a file, beginning with the 5th line from the last line, and ending with the last line. In your case, you can set OFFSET = -1instead of OFFSET = -5.

    (( OFFSET = -5 )); (( N1 = $(cat pgen.c | wc -l) + OFFSET )); sed -n "$N1,\$p" thefile
    

    This command can be entered in one line.

提交回复
热议问题