sed and awk.

Hints UNIX

In my latest bit of training, I learned about sed and awk.
sed is a line editor (stream editor) (scripting with ed).
awk is a pattern-directed scanning and processing language and divides each line to fields by separating words by spaces or other specified field separators.

Some tidbits about using sed to quickly edit files:

  • To use multiple commands in one line, separate the commands by semi-colons ( ex. sed -n '/jappler/s/=/!/g ; /jappler/p' jappler.txt) the example command will look for all lines with jappler and on those lines, it will replace all equal signs with exclamation points in the text document jappler.txt, then print (-p) all occurances of jappler.
  • To save changes to files, you would perform your sed command and redirect it to another file (sed '/jappler/s/=/!/g jappler.txt > jappler2.txt). If you wanted the new text in the original file, you could use: mv jappler2.txt > jappler.txt.
  • Instead of using multiple commands separated with semicolons (let’s say you want to reuse the commands), you can also list the commands in a file (ex. commands.sed) and then call the file when you want to issue the commands. (ex. sed -nf commands.sed jappler.txt (-n is used when you do not want all the lines printed) (-f is to specify a file)

Some general tidbits I found useful while learning awk:

  • Let’s say you see your crazy friend Ken on your server and he is is running all kinds of processes, but you are pissed off that he forgot your birthday so you decide to kill all of his processes…you can use awk for that: kill -9 `ps aux |awk '/ken/ { print $2 }'`. The first part of the command gets a list of all the processes Ken is running, and then that output is piped to awk where it looks at the second field (user name) and then kills all processes that Ken is running. 😉
  • uniq is a UNIX command that will remove duplicates from a sorted list (sort command will sort)
  • wc is a UNIX command that will count words (word count) and adding a -l will show the number of lines.

Leave a Reply

Your email address will not be published.
*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

jappler.com

jappler.com v5

Well, now that I have spent countless hours working on mastering CSS and also getting back to my color scheme of choice (not sure what it is? you will have to wait) I am getting ready to release yet another version of jappler.com. I am finished with my site style sheet for NS browsers and […]

Hints UNIX

regular expressions help.

No, the site was not hacked, I chose to write about regular expressions. I am in my last section of my UNIX/Linux Systems Administrator training and I am equally excited about regular expressions as I was about learning subnets. I know regular expressions are very powerful, and I remember enjoying subnets after I finally figured […]