Skip to content
Advertisement

Bash sort ignore first 5 lines

I’m having trouble ignoring the first 5 lines of my file while sorting the rest. My current command sorts the entire file by the second item, however I need to skip the first 5 “header” lines. I need read it and write it to the same file.

Current Command

sort -f -t $ -k2n,2 -o /folder/File.txt /folder/File.txt

Example

2016/07/07 15:41:02
@24921
@
@
@-1
b$1$4$...
a$2$5$...

Advertisement

Answer

This sorts lines 6 and after of the file while leaving the first 5 lines unchanged:

{ head -n5 file.txt; tail -n+6 file.txt | sort -ft$ -k2n,2; } >file.tmp && mv file.tmp file.txt

tcsh

Unlike bash, ksh, and zsh, tcsh does not support command grouping with {...}. Instead try a subshell:

( head -n5 file.txt; tail -n+6 file.txt | sort -ft$ -k2n,2 ) >file.tmp && mv file.tmp file.txt
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement