How to sort a file in-place

后端 未结 6 1830
青春惊慌失措
青春惊慌失措 2020-11-30 17:37

When we use sort file command, the file shows its contents in a sorted way what if I don\'t want to get any-kind of output but a sorted file?

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 18:38

    Here's an approach which (ab)uses vim:

    vim -c :sort -c :wq -E -s "${filename}"
    

    The -c :sort -c :wq portion invokes commands to vim after the file opens. -E and -s are necessary so that vim executes in a "headless" mode which doesn't draw to the terminal.

    This has almost no benefits over the sort -o "${filename}" "${filename}" approach except that it only takes the filename argument once.


    This was useful for me to implement a formatter directive in a nanorc entry for .gitignore files. Here's what I used for that:

    syntax "gitignore" "\.gitignore$"
    
    formatter vim -c :sort -c :wq -E -s
    

提交回复
热议问题