Skip to main content
added 2 characters in body
Source Link
Yurko
  • 738
  • 3
  • 4

UPDATED according to changed task.

File file1 with your data created:

$ cat file1
1    10  11
10   200 201
2    20   21
22   2000 2001

Now we sort the file lines and write results to file2:

$ cat file1|sort -n > file2

Or we can even simplify it:

$ sort -n file1 > file2

Checking the results in file2:

$ cat file2
1    10  11
2    20   21
10   200 201
22   2000 2001

UPDATED according to changed task.

File file1 with your data created:

$ cat file1
1    10  11
10   200 201
2    20   21
22   2000 2001

Now we sort the file lines and write results to file2:

$ cat file1|sort -n > file2

Or we can even simplify it:

sort -n file1 > file2

Checking the results in file2:

$ cat file2
1    10  11
2    20   21
10   200 201
22   2000 2001

UPDATED according to changed task.

File file1 with your data created:

$ cat file1
1    10  11
10   200 201
2    20   21
22   2000 2001

Now we sort the file lines and write results to file2:

$ cat file1|sort -n > file2

Or we can even simplify it:

$ sort -n file1 > file2

Checking the results in file2:

$ cat file2
1    10  11
2    20   21
10   200 201
22   2000 2001
deleted 398 characters in body
Source Link
Yurko
  • 738
  • 3
  • 4

UPDATED according to changed task.

File file1 with your data created:

for i$ incat file1
1 10 11 12 13 1410 15 1611
10 17 18 19200 201
2 20 21 22 3 420 5 6 721
22 8 9;do echo2000 $i;done|sort
1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
3
4
5
6
7
8
92001

Now we sort the file lines and write results to file2:

for i in 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3$ 4cat 5file1|sort 6-n 7> 8file2

Or we can even simplify it:

sort 9;do-n echofile1 $i;done|sort> -n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22file2

You can extend numbers with leading zeroes,Checking the results in this cases sort and sort -nfile2 results the same output:

for i$ incat file2
1 10 11 12 13 14 15 16 17 1810 19 11
2 20 21 22 320 4 5 621
10 7 8 9; do200 printf201
22 "%02d\n" $i ;2000 done|sort
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
222001
for i in 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9;do echo $i;done|sort
1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
3
4
5
6
7
8
9
for i in 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9;do echo $i;done|sort -n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

You can extend numbers with leading zeroes, in this cases sort and sort -n results the same output:

for i in 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9; do printf "%02d\n" $i ; done|sort
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22

UPDATED according to changed task.

File file1 with your data created:

$ cat file1
1    10  11
10   200 201
2    20   21
22   2000 2001

Now we sort the file lines and write results to file2:

$ cat file1|sort -n > file2

Or we can even simplify it:

sort -n file1 > file2

Checking the results in file2:

$ cat file2
1    10  11
2    20   21
10   200 201
22   2000 2001
Source Link
Yurko
  • 738
  • 3
  • 4

for i in 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9;do echo $i;done|sort
1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
3
4
5
6
7
8
9
for i in 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9;do echo $i;done|sort -n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

You can extend numbers with leading zeroes, in this cases sort and sort -n results the same output:

for i in 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9; do printf "%02d\n" $i ; done|sort
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22