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

Since this is posted this on unix.stackexchange.com, I am going to assume that you have access to the usual unix tools.

Alphabetic sorting on first column:

$ sort file.txt >alpha_sorted.txt $ cat alpha_sorted.txt d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 

Numeric sorting:

$ sort -nk2,2 file.txt >numbers_sorted.txt $ cat numbers_sorted.txt g 1 h 1 f 2 f 2 s 4 d 5 g 5 s 5 d 9 g 10 d 29 

-n specifies numeric sorting. -k2,2 specifies sorting on the second column.

For more information, see man sort.

Problems editing a Unix script with Notepad

I created a script with DOS line-endings:

$ cat dos.sh sort file.txt >alpha_sorted.txt cat alpha_sorted.txt 

Although it is not visible, I added a space at the end of the cat command. With this file, I can reproduce the error that you saw:

$ chmod +x dos.sh $ dos.sh cat: alpha_sorted.txt: No such file or directory : No such file or directory 

We can correct this problem with a utility such as dos2unix or tr. Using tr:

$ tr -d '\r' <dos.sh >fixed.sh $ chmod +x fixed.sh 

Now, we can run the command successfully:

$ fixed.sh d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 

Since this is posted this on unix.stackexchange.com, I am going to assume that you have access to the usual unix tools.

Alphabetic sorting on first column:

$ sort file.txt >alpha_sorted.txt $ cat alpha_sorted.txt d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 

Numeric sorting:

$ sort -nk2,2 file.txt >numbers_sorted.txt $ cat numbers_sorted.txt g 1 h 1 f 2 f 2 s 4 d 5 g 5 s 5 d 9 g 10 d 29 

-n specifies numeric sorting. -k2,2 specifies sorting on the second column.

For more information, see man sort.

Since this is posted this on unix.stackexchange.com, I am going to assume that you have access to the usual unix tools.

Alphabetic sorting on first column:

$ sort file.txt >alpha_sorted.txt $ cat alpha_sorted.txt d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 

Numeric sorting:

$ sort -nk2,2 file.txt >numbers_sorted.txt $ cat numbers_sorted.txt g 1 h 1 f 2 f 2 s 4 d 5 g 5 s 5 d 9 g 10 d 29 

-n specifies numeric sorting. -k2,2 specifies sorting on the second column.

For more information, see man sort.

Problems editing a Unix script with Notepad

I created a script with DOS line-endings:

$ cat dos.sh sort file.txt >alpha_sorted.txt cat alpha_sorted.txt 

Although it is not visible, I added a space at the end of the cat command. With this file, I can reproduce the error that you saw:

$ chmod +x dos.sh $ dos.sh cat: alpha_sorted.txt: No such file or directory : No such file or directory 

We can correct this problem with a utility such as dos2unix or tr. Using tr:

$ tr -d '\r' <dos.sh >fixed.sh $ chmod +x fixed.sh 

Now, we can run the command successfully:

$ fixed.sh d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 
added 4 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Since this is posted this on unix.stackexchange.com, I am going to assume that you have access to the usual unix tools.

Alphabetic sorting on first column:

$ sort file.txt >alpha_sorted.txt $ cat alpha_sorted.txt d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 

Numeric sorting:

$ sort -nk2,2 file.txt >numbers_sorted.txt $ cat numbers_sorted.txt g 1 h 1 f 2 f 2 s 4 d 5 g 5 s 5 d 9 g 10 d 29 

-n specifies numeric sorting. -k2,2 specifies sorting on the second column.

For more information, see man sort.

Since this is posted this on unix.stackexchange.com, I am going to assume that you have access to the usual unix tools.

Alphabetic sorting on first column:

$ sort file.txt >alpha_sorted.txt $ cat alpha_sorted.txt d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 

Numeric sorting:

$ sort -nk2 file.txt >numbers_sorted.txt $ cat numbers_sorted.txt g 1 h 1 f 2 f 2 s 4 d 5 g 5 s 5 d 9 g 10 d 29 

-n specifies numeric sorting. -k2 specifies sorting on the second column.

For more information, see man sort.

Since this is posted this on unix.stackexchange.com, I am going to assume that you have access to the usual unix tools.

Alphabetic sorting on first column:

$ sort file.txt >alpha_sorted.txt $ cat alpha_sorted.txt d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 

Numeric sorting:

$ sort -nk2,2 file.txt >numbers_sorted.txt $ cat numbers_sorted.txt g 1 h 1 f 2 f 2 s 4 d 5 g 5 s 5 d 9 g 10 d 29 

-n specifies numeric sorting. -k2,2 specifies sorting on the second column.

For more information, see man sort.

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

Since this is posted this on unix.stackexchange.com, I am going to assume that you have access to the usual unix tools.

Alphabetic sorting on first column:

$ sort file.txt >alpha_sorted.txt $ cat alpha_sorted.txt d 29 d 5 d 9 f 2 f 2 g 1 g 10 g 5 h 1 s 4 s 5 

Numeric sorting:

$ sort -nk2 file.txt >numbers_sorted.txt $ cat numbers_sorted.txt g 1 h 1 f 2 f 2 s 4 d 5 g 5 s 5 d 9 g 10 d 29 

-n specifies numeric sorting. -k2 specifies sorting on the second column.

For more information, see man sort.