head
Head is a program that prints the first so many lines of it's input. By default it will print the first 10 lines but we may modify this with a command line argument.
head [-number of lines to print] [path]
- head mysampledata.txt
- Fred apples 20
- Susy oranges 5
- Mark watermellons 12
- Robert pears 4
- Terry oranges 9
- Lisa peaches 7
- Susy oranges 12
- Mark grapes 39
- Anne mangoes 7
- Greg pineapples 3
Above was head's default behaviour. And below is specifying a set number of lines.
- head -4 mysampledata.txt
- Fred apples 20
- Susy oranges 5
- Mark watermellons 12
- Robert pears 4
tail
Tail is the opposite of head. Tail is a program that prints the last so many lines of it's input. By default it will print the last 10 lines but we may modify this with a command line argument.
tail [-number of lines to print] [path]
- tail mysampledata.txt
- Mark watermellons 12
- Robert pears 4
- Terry oranges 9
- Lisa peaches 7
- Susy oranges 12
- Mark grapes 39
- Anne mangoes 7
- Greg pineapples 3
- Oliver rockmellons 2
- Betty limes 14
Above was tail's default behaviour. And below is specifying a set number of lines.
- tail -3 mysampledata.txt
- Greg pineapples 3
- Oliver rockmellons 2
- Betty limes 14
sort
Sort will sort it's input, nice and simple. By default it will sort alphabetically but there are many options available to modify the sorting mechanism. Be sure to check out the man page to see everything it may do.
sort [-options] [path]
- sort mysampledata.txt
- Anne mangoes 7
- Betty limes 14
- Fred apples 20
- Greg pineapples 3
- Lisa peaches 7
- Mark grapes 39
- Mark watermellons 12
- Oliver rockmellons 2
- Robert pears 4
- Susy oranges 12
- Susy oranges 5
- Terry oranges 9
nl
nl stands for number lines and it does just that.
nl [-options] [path]
- nl mysampledata.txt
- 1 Fred apples 20
- 2 Susy oranges 5
- 3 Mark watermellons 12
- 4 Robert pears 4
- 5 Terry oranges 9
- 6 Lisa peaches 7
- 7 Susy oranges 12
- 8 Mark grapes 39
- 9 Anne mangoes 7
- 10 Greg pineapples 3
- 11 Oliver rockmellons 2
- 12 Betty limes 14
The basic formatting is ok but sometimes you are after something a little different. With a few command line options, nl is happy to oblige.
- nl -s '. ' -w 10 mysampledata.txt
- 1. Fred apples 20
- 2. Susy oranges 5
- 3. Mark watermellons 12
- 4. Robert pears 4
- 5. Terry oranges 9
- 6. Lisa peaches 7
- 7. Susy oranges 12
- 8. Mark grapes 39
- 9. Anne mangoes 7
- 10. Greg pineapples 3
- 11. Oliver rockmellons 2
- 12. Betty limes 14
In the above example we have used 2 command line options. The first one -s specifies what should be printed after the number while the second one -w specifies how much padding to put before the numbers. For the first one we needed to include a space as part of what was printed. Because spaces are normally used as separator characters on the command line we needed a way of specifying that the space was part of our argument and not just inbetween arguments. We did that by including the argument surrounded by quotes.
wc
wc stands for word count and it does just that (as well as characters and lines). By default it will give a count of all 3 but using command line options we may limit it to just what we are after.
wc [-options] [path]
- wc mysampledata.txt
- 12 36 195 mysampledata.txt
Sometimes you just want one of these values. -l will give us lines only, -w will give us words and -m will give us characters. The example below gives us just a line count.
- wc -l mysampledata.txt
- 12 mysampledata.txt
You may combine the command line arguments too. This example gives us both lines and words.
- wc -lw mysampledata.txt
- 12 36 mysampledata.txt
cut
cut is a nice little program to use if your content is separated into fields (columns) and you only want certain fields.
cut [-options] [path]
In our sample file we have our data in 3 columns, the first is a name, the second is a fruit and the third an amount. Let's say we only wanted the first column.
- cut -f 1 -d ' ' mysampledata.txt
- Fred
- Susy
- Mark
- Robert
- Terry
- Lisa
- Susy
- Mark
- Anne
- Greg
- Oliver
- Betty
cut defaults to using the TAB character as a separator to identify fields. In our file we have used a single space instead so we need to tell cut to use that instead. The separator character may be anything you like, for instance in a CSV file the separator is typically a comma ( , ). This is what the -d option does (we include the space within single quotes so it knows this is part of the argument). The -f option allows us to specify which field or fields we would like. If we wanted 2 or more fields then we separate them with a comma as below.
- cut -f 1,2 -d ' ' mysampledata.txt
- Fred apples
- Susy oranges
- Mark watermellons
- Robert pears
- Terry oranges
- Lisa peaches
- Susy oranges
- Mark grapes
- Anne mangoes
- Greg pineapples
- Oliver rockmellons
- Betty limes
sed
sed stands for Stream Editor and it effectively allows us to do a search and replace on our data. It is quite a powerful command but we will use it here in it's basic format.
sed <expression> [path]
A basic expression is of the following format:
- s/search/replace/g
The initial s stands for substitute and specifies the action to perform (there are others but for now we'll keep it simple). Then between the first and second slashes ( / ) we place what it is we are searching for. Then between the second and third slashes, what it is we wish to replace it with. The g at the end stands for global and is optional. If we omit it then it will only replace the first instance of search on each line. With the g option we will replace every instance of search that is on each line. Let's see an example. Say we ran out of oranges and wanted to instead give those people bananas.
- sed 's/oranges/bananas/g' mysampledata.txt
- Fred apples 20
- Susy bananas 5
- Mark watermellons 12
- Robert pears 4
- Terry bananas 9
- Lisa peaches 7
- Susy bananas 12
- Mark grapes 39
- Anne mangoes 7
- Greg pineapples 3
- Oliver rockmellons 2
- Betty limes 14
It's important to note that sed does not identify words but strings of characters. Try running the example above yourself but replacing oranges with es and you'll see what I mean. The search term is also actually something called a regular expression which is a means to define a pattern (similar to wildcards we looked at in section 7). We'll learn more about regular expressions in the next section and you can use them to make sed even more powerful.
Also note that we included our expression within single quotes. We did this so that any characters included in it which may have a special meaning on the command line don't get interpreted and acted upon by the command line but instead get passed through to sed.
A common mistake is to forget the single quotes in which case you may get some strange behaviour from the command line. If this happens you may need to press CTRL+c to cancel the program and get back to the prompt.
uniq
uniq stands for unique and it's job is to remove duplicate lines from the data. One limitation however is that those lines must be adjacent (ie, one after the other). (sometimes this is not the case but we'll see one way we can fix this in Section 11 Piping and Redirection).
uniq [options] [path]
Let's say that our sample file was actually generated from another sales program but after a software update it had some buggy output.
- cat mysampledata.txt
- Fred apples 20
- Susy oranges 5
- Susy oranges 5
- Susy oranges 5
- Mark watermellons 12
- Robert pears 4
- Terry oranges 9
- Lisa peaches 7
- Susy oranges 12
- Mark grapes 39
- Mark grapes 39
- Anne mangoes 7
- Greg pineapples 3
- Oliver rockmellons 2
- Betty limes 14
No worries, we can easily fix that using uniq.
- uniq mysampledata.txt
- Fred apples 20
- Susy oranges 5
- Mark watermellons 12
- Robert pears 4
- Terry oranges 9
- Lisa peaches 7
- Susy oranges 12
- Mark grapes 39
- Anne mangoes 7
- Greg pineapples 3
- Oliver rockmellons 2
- Betty limes 14
tac
Linux guys are known for having a funny sense of humor. The program tac is actually cat in reverse. It was named this as it does the opposite of cat. Given data it will print the last line first, through to the first line.
tac [path]
Maybe our sample file is generated by writing each new order to the end of the file. As a result, the most recent orders are at the end of the file. We would like it the other way so that the most recent orders are always at the top.
- tac mysampledata.txt
- Betty limes 14
- Oliver rockmellons 2
- Greg pineapples 3
- Anne mangoes 7
- Mark grapes 39
- Susy oranges 12
- Lisa peaches 7
- Terry oranges 9
- Robert pears 4
- Mark watermellons 12
- Susy oranges 5
- Fred apples 20
0 comments:
Post a Comment