Skip to main content
added 5 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Here is an example that sorts all lines but the first:

#!/bin/awk -f
BEGIN{cmd="sort"}
NR==1{print;next}
{print $1,$2 | cmd}
END{close(cmd)}

Example

Consider this file:

$ cat file
Letter  Value
A       12
D       10
C       15
B       13

Then:

$ awk -f script.awk file
Letter  Value
A 12
B 13
C 15
D 10

The first input line is the first output line. The remaining lines are sorted.

Here is an example that sorts all lines but the first:

#!/bin/awk -f
BEGIN{cmd="sort"}
NR==1{print;next}
{print $1,$2 | cmd}
END{close(cmd)}

Example

Consider this file:

$ cat file
Letter  Value
A       12
D       10
C       15
B       13

Then:

$ awk -f script.awk file
Letter  Value
A 12
B 13
C 15
D 10

The first input is the first output line. The remaining lines are sorted.

Here is an example that sorts all lines but the first:

#!/bin/awk -f
BEGIN{cmd="sort"}
NR==1{print;next}
{print $1,$2 | cmd}
END{close(cmd)}

Example

Consider this file:

$ cat file
Letter  Value
A       12
D       10
C       15
B       13

Then:

$ awk -f script.awk file
Letter  Value
A 12
B 13
C 15
D 10

The first input line is the first output line. The remaining lines are sorted.

Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Here is an example that sorts all lines but the first:

#!/bin/awk -f
BEGIN{cmd="sort"}
NR==1{print;next}
{print $1,$2 | cmd}
END{close(cmd)}

Example

Consider this file:

$ cat file
Letter  Value
A       12
D       10
C       15
B       13

Then:

$ awk -f script.awk file
Letter  Value
A 12
B 13
C 15
D 10

The first input is the first output line. The remaining lines are sorted.