Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux sort only by the first letter of each line

Tags:

linux

sorting

what's the fastest way to sort file by the first letter of each line? should one use linux sort command or are there other more efficient alternatives? if I am using sort, how do I sort only by the first letter and ignore the rest?

like image 658
user775187 Avatar asked Jun 09 '11 19:06

user775187


People also ask

How do I sort lines in Linux?

To sort lines of text files, we use the sort command in the Linux system. The sort command is used to prints the lines of its input or concatenation of all files listed in its argument list in sorted order. The operation of sorting is done based on one or more sort keys extracted from each line of input.

How do I sort a line alphabetically in Linux?

In the Linux system, you will find one command named sort. This command can sort your data alphabetically. Here flag -k is used to select a field for sorting.

How do I sort by name in Linux?

If you add the -X option, ls will sort files by name within each extension category. For example, it will list files without extensions first (in alphanumeric order) followed by files with extensions like . 1, . bz2, .

How do I sort in descending order in Linux?

the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default.


1 Answers

This will sort by the first character

sort -k1.1,1.1 filename

add -s if you want to preserve the input order of lines with the same first character

like image 102
Jim Garrison Avatar answered Nov 18 '22 15:11

Jim Garrison