![]() The best way to learn command line behavior is to experiment. And for some tasks, there are no graphical equivalents. Now that you've had a taste of the command line's power, do you think you can handle the responsibility? Command line utilities have more flexibility than their graphical counterparts do. Remember that the pipe operator and the file redirect operators do different things. $ cat contacts.txt | sort -k2 | uniq > contact_list.txt To sort the file, filter unique entries, and then save the new file, use this: This command only displays unique entries, but it doesn't save the results. If you have a long contact list and you think that this list contains duplicate entries, you can pipe your list to sort and then uniq to remove those duplicates: You can sort by the second column with the -k switch, specifying the column you want to sort on, which in our list is column two: Was the output for either of these what you expected? By default, the sort command performs a dictionary sort on the first word or column, which is why Bob Jones and Terry Jones are not listed one after the other. ![]() You can reverse the sort order with the -r switch: First, let's look at the contents as they are in the file before sorting: Say that you want to sort the list of names in contacts.txt. The -v switch tells the grep command to invert or ignore lines that contain the string that follows-in this case any line containing the word "grep." Sorting resultsĪnother popular use of the pipe operator is to sort your results by piping to the sort command. $ ps -ef | grep systemd | awk '' | grep -v grep To list the process IDs (PIDs) for all systemd-related processes: You can also perform complex tasks using pipes. For example, you can list the number of files in the /etc directory with this:Īgain, your results might look different, but you know something is wrong if the command returns a small number of files. This listing is from a fresh "RHEL 8 server no GUI" install. The -l switch displays the number of lines. An easy option is to pipe the results of your ls command to the wc (word count) command: You still need to find out how many there are, and a visual count would take a long time. The results are too long to list here, but as you can see from your displayed list, there are a lot of them. Maybe you want to find out how many directories under /etc are writeable by root: You could also grep for "RUNNING" or "RUN" to display the same information. Note: Don't confuse pipe ( |) redirection with file redirection ( >) and ( file or cmd The pipe redirects that output as input to cmd2. When you pipe one command's output to another, however, the information from cmd1 doesn't produce output to the screen. ![]() Let's look at a theoretical example as an illustration of how this process works:īoth cmd1 and cmd2 are command line utilities that output their results to the screen ( stdout). You can pipe the output to any command that accepts stream input. This purpose is why the most popular use for pipes involves the commands grep and sort. You use piping to filter the contents of a large file-to find a particular string or word, for example. One of the main purposes of piping is filtering. And, you're not limited to a single piped command-you can stack them as many times as you like, or until you run out of output or file descriptors. The pipe takes output from one command and uses it as input for another. ![]() One of the most powerful shell operators is the pipe ( |). It is an efficient way of quickly gathering and processing information, creating new scripts, and configuring systems. The Linux command line is alive and well, and still going strong. I'm sorry to inform you, but the command line didn't die off with the dinosaurs, nor did it disappear with the dodo or the carrier pigeon. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |