-1

I have a YAML file with thousands of lines of text in it, and I want the most recent lines added to appear on the very top of the file and the oldest contents to be at the very bottom of the file, so that when I open the file I don't have to scroll down for 5 minutes to get to the bottom to be able to read the new content.

#current structure        #desired structure
1: oldest line            4: newest line
2: old line               3: new line 
3: new line               2: old line
4: newest line            1: oldest line
Saber
  • 107
  • 1
  • 10

1 Answers1

-1

One solution, taking care of comments :

 Command

head -1 file; awk 'NR>1' file | sort -nr

 Output

#current structure
4: newest line
3: new line       
2: old line       
1: oldest line 

 Finally to overwrite file

head -1 file; awk 'NR>1' file | sort -nr | tee file

(works for small files.)

For big files :

 head -1 file; awk 'NR>1' file | sort -nr > temp$$
 mv temp$$ file
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223