1. Home
  2. Computing & Technology
  3. Linux

GNU/Linux Command-Line Tools Summary

From


   GNU/Linux Command-Line Tools Summary
   Prev    Chapter 11. Text Related Tools    Next

11.4. Text manipulation tools


       Also see
        

Also see tac , and cat over in this section, Section 11.2 , as they can perform text manipulation too

  • sort
  •    

    Sorting text with no options the sort is alphabetical. Can be run on text files to sort them alphabetically (note it also concatenates files), can also be used with a pipe '|' to sort the output of a command.

    Use sort -r to reverse the sort output, use the -g option to sort 'numerically' (ie read the entire number, not just the first digit).

    Examples:


       

    cat shoppinglist.txt | sort

    The above command would run cat on the shopping list then sort the results and display them in alphabetical order.


       

    sort -r shoppinglist.txt

    The above command would run sort on a file and sort the file in reverse alphabetical order.

    Advanced sort commands:

    sort is a powerful utility, here are some of the more hard to learn (and lesser used) commands. Use the -t option to use a particular symbol as the separator then use the -k option to specify which column you would like to sort by, where column 1 is the first column before the separator. Also use the -g option if numeric sorting is not working correctly (without the -g option sort just looks at the first digit of the number). Here is a complex example:


       

    sort -t : -k 4 -k 1 -g /etc/passwd | more

    This will sort the "/etc/passwd" file, using the colon ':' as the separator. It will sort via the 4th column (GID section, in the file) and then sort within that sort using the first (name) to resolve any ties. The -g is there so it sorts via full numbers, otherwise it will have 4000 before 50 (it will just look at the first digit...).


  • join
  •    

    Will put two lines together assuming they share at least one common value on the relevant line. It won't print lines if they don't have a common value.

    Command syntax:


       

    join file1 file2


  • cut
  •    

    Prints selected parts of lines (of a text file), or, in other words, removes certain sections of a line. You may wish to remove things according to tabs or commas, or anything else you can think of...

    Options for cut:

    • -d --- allows you to specify another delimiter, for example ':' is often used with /etc/passwd:


         

      cut -d ':' (and probably some more options here) /etc/passwd

    • -f --- this option works with the text by columns, separated according to the delimiter. For example if your file had lines like "result,somethingelse,somethingelse" and you only wanted result you would use:


         

      cut -d ',' -f 1 /etc/passwd

      This would get you only the usernames in /etc/passwd

    • "," (commas) --- used to separate numbers, these allow you to cut particular columns. For example:


         

      cut -d ':' -f 1,7 /etc/passwd

      This would only show the username and the shell that each person is setup for in /etc/passwd.

    • "-" (hyphen) --- used to show from line x to line y, for example 1-4, (would be from lines 1 to line 4).


         

      cut -c 1-50 file1.txt

      This would cut (display) characters (columns) 1 to 50 of each line (and anything else on that line is

* License

* GNU/Linux Command-Line Tools Summary Index

Explore Linux
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Linux
  4. Linux Documentation
  5. Linux Tools Summary
  6. GNU/Linux Command-Line Tools Summary - Text manipulation tools

©2009 About.com, a part of The New York Times Company.

All rights reserved.