AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Bash awk sed grep tutorial12/31/2023 ![]() sudo netstat -anp | wc -l : "Find all the open network sockets, then tell me how many total lines of text you'd have used to tell me.". ![]() sudo netstat -anp | head -n5 : "Find all the open network sockets, but limit output to the first five lines.".So we can translate the four commands above into plain English: There's also wc, which, with the argument -l, tells you how many lines of text hit its standard input. We introduced some new commands above: head, which limits output to the first n lines and then truncates it. Tcp6 0 0 :::80 :::* LISTEN sudo netstat -anp | head -n2 sudo netstat -anp | grep apache Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name So: sudo netstat -anp | head -n5Īctive Internet connections (servers and established) In this case, what we want to know about is the apache webserver. Instead, we can use grep to return only the lines we really want. Or, you might instead redirect it to a file to be opened with a text editor: netstat -anp > netstat.txt.īut there's a better option. You could just pipe all that output to a pager, so you can read it one page at a time, with netstat -anp | less. Unfortunately, this produces a lot of output-frequently, several tens of pages. Typically, we'd invoke netstat using the -anp arguments-for all sockets, numeric display, and displaying the owning pid of each socket. Many utilities can accomplish this goal netstat is one of the older and better-known options. Advertisementįor example, let's say you're interested in finding which ports the apache web server has open on your system. In that vein, grep is the tool you use to find lines that contain a particular string of text. When first learning about tools like grep, I find it helps to think of them as far simpler than they truly are. They make it possible to treat individual, simple utilities as part of a greater whole.Īnd that last concept-breaking one complex task into several simpler tasks-is equally necessary to learning to think in complex command-line invocations in the first place! Grep finds strings Understanding these redirection operators is crucial to understanding the kinds of wizardly command lines you're presumably here to learn. Pipe the standard output of the thing on the left into the standard input of the thing on the right.Įcho "test123" | mail -s "subjectline" emailaddress Ls /home/me > myfilestwice.txt ls /home/me > myfilestwice.txt Ls /home/me > myfilesonce.txt ls /home/me > myfilesonce.txtĪppend the output of the thing on the left to the end of the existing file on the right. Place the output of the thing on the left in the empty file named on the right. Process the command on the right after you're done processing the command on the left. ![]() Again, we're going to keep this very simple: Operator Redirection 101īefore we can talk about sed, awk, and grep, we need to talk about something a bit more basic-command-line redirection. Instead of giving you encyclopedic listings of every possible argument and use case for each of these ubiquitous commands, we're going to teach you how to think about them-and how to easily, productively incorporate them in your own daily command-line use. If you find yourself in the same boat, grab a beverage and buckle in. For example, the sed man page weighs in at around 1,800 words alone without ever really explaining how regular expressions work or the most common uses of sed itself. Trying to read the man pages for the utilities most frequently seen in these extended command chains didn't make them seem more approachable, either. Surely, this was just an exercise in e-braggadocio, right? Honestly, I doubted the real-world value of that. ![]() Authors and commenters might chain 10 commands together with pipes and angle brackets-something I never did in day-to-day system administration. Linux/BSD command line wizardry: Learn to think in sed, awk, and grepĪs a relatively isolated junior sysadmin, I remember seeing answers on Experts Exchange and later Stack Exchange that baffled me.Command line wizardry, part two: Variables and loops in Bash.Jozefmicic via Getty Images reader comments 492 with Command Line Wizardry ![]()
0 Comments
Read More
Leave a Reply. |