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.