Delete third-to-last line of file using sed or awk

With tac + awk solution, could you please try following. Just set line variable of awk to line(from bottom) whichever you want to skip.

tac Input_file | awk -v line="3" 'line==FNR{next} 1' | tac

Explanation: Using tac will read the Input_file reverse(from bottom line to first line), passing its output to awk command and then checking condition if line is equal to line(which we want to skip) then don't print that line, 1 will print other lines.

2nd solution: With awk + wc solution, kindly try following.

awk -v lines="$(wc -l < Input_file)" -v skipLine="3" 'FNR!=(lines-skipLine+1)' Input_file

Explanation: Starting awk program here and creating a variable lines which has total number of lines present in Input_file in it. variable skipLine has that line number which we want to skip from bottom of Input_file. Then in main program checking condition if current line is NOT equal to lines-skipLine+1 then printing the lines.

3rd solution: Adding solution as per Ed sir's comment here.

awk -v line=3 '{a[NR]=$0} END{for (i=1;i<=NR;i++) if (i != (NR-line)) print a[i]}' Input_file

Explanation: Adding detailed explanation for 3rd solution.

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.

With ed

ed -s ip.txt <<< $'$-2d\nw'

# thanks Shawn for a more portable solution
printf '%s\n' '$-2d' w | ed -s ip.txt

This will do in-place editing. $ refers to last line and you can specify a negative relative value. So, $-2 will refer to last but second line. w command will then write the changes.

See ed: Line addressing for more details.


This might work for you (GNU sed):

sed '1N;N;$!P;D' file

Open a window of 3 lines in the file then print/delete the first line of the window until the end of the file.

At the end of the file, do not print the first line in the window i.e. the 3rd line from the end of the file. Instead, delete it, and repeat the sed cycle. This will try to append a line after the end of file, which will cause sed to bail out, printing the remaining lines in the window.

A generic solution for n lines back (where n is 2 or more lines from the end of the file), is:

sed ':a;N:s/[^\n]*/&/3;Ta;$!P;D' file 

Of course you could use:

tac file | sed 3d | tac

But then you would be reading the file 3 times.

Tags:

Awk

Sed

Ed