Sort text file in Linux from a particular position

I have a text file, named all.txt, with the following content:
f http://www.google.com/
f http://www.abc.com/
c http://www.google.com/
c http://www.abc.com/

I want to sort it on the URL ignoring the first 2 characters i.e f or c .

To do this, Linux provides a sort command. Using the keydef option, the file can be sorted. To do so, open a terminal window and enter the command below:
$ sort -k1.2 all.txt <enter>

where 1 is the field, and 2 is the character position from where the sort should start. The -k1.2 parameter is the keydef.

The above command will dump the sorted output on the screen. To put it in another file, enter the command below:
$ sort -k1.2 all.txt > sortedall.txt <enter>

where sortedall.txt is the file with the sorted data.

To view the options available with sort command in Linux, read the man pages for sort.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s