Remove last lines from file with shell, bash, csh, zsh

 IT, Linux  Kommentare deaktiviert für Remove last lines from file with shell, bash, csh, zsh
Jan 212016
 

To remove the last X lines of a file, the easiest way is to use head combined with wc -l

# get the output of wc -l (which counts the lines of a file), parse them to a number 
# and extract 3 (the number of lines you want to remove)
philipp@Host% set CMD1="expr `cat longFile.csv | wc -l` - 3"

# now remove last 3 lines by using head and redirect them to your file
philipp@Host% head -n  `$CMD1` Segment5_Contexts.csv > shortFile.csv