我有一个YAML文件,其中包含数千行文本,并且我希望添加的最新行出现在文件的顶部,而最旧的内容出现在文件的底部,以便当我打开文件时,无需向下滚动5分钟即可到达底部才能读取新内容。
#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
答案 0 :(得分:-1)
一种解决方案,照顾评论:
head -1 file; awk 'NR>1' file | sort -nr
#current structure
4: newest line
3: new line
2: old line
1: oldest line
head -1 file; awk 'NR>1' file | sort -nr | tee file
(适用于小文件。)
对于大文件:
head -1 file; awk 'NR>1' file | sort -nr > temp$$
mv temp$$ file