並べ替えますが、ヘッダー行を一番上に保つ

並べ替えますが、ヘッダー行を一番上に保つ

まず、列ヘッダーの束である行を生成してから、データ行の束を生成するプログラムから出力を取得しています。この出力の個々の列を切り取り、個々の列に基づいてソートされていることを確認したいと思います。ヘッダーがない場合は、列のサブセットを-k選択または確認して、切り取りと並べ替えを簡単に実行できます。ただし、このソート方法は列ヘッダーを残りの出力行と混在させます。タイトルを一番上に保つ簡単な方法はありますか?sortcutawk

ベストアンサー1

Andyのアイデアを盗んで、使いやすくするために関数に変換します。

# print the header (the first line of input)
# and then run the specified command on the body (the rest of the input)
# use it in a pipeline, e.g. ps | body grep somepattern
body() {
    IFS= read -r header
    printf '%s\n' "$header"
    "$@"
}

今できます:

$ ps -o pid,comm | body sort -k2
  PID COMMAND
24759 bash
31276 bash
31032 less
31177 less
31020 man
31167 man
...

$ ps -o pid,comm | body grep less
  PID COMMAND
31032 less
31177 less

おすすめ記事