Friday, August 19, 2016

Up and Running with Bash Scripting - 2

Pipes and Redirection:
Pipes: Piping takes the result of one command and sends, or pipes it into another command.

example : ls | more

Redirection: Redirection works with a standard input, standard output and standard error

example: cp -v * ../otherfolder 1> ../success.txt 2> ../error.txt

-v : tells the cp command to be verbose, so we see each copy operation
*  : specifies file name expansion to match all of the files
>  : The greater than symbol represents redirecting the output from each of those somewhere else. So the successes will go to a file called success.txt. And the errors will go into a file called error.txt.
1 & 2 numbers: represent the standard output and standard error, respectively

Manipulating output with grep, awk and cut
Grep: It is handy tool which lets you search files for specific patterns of text. Enable the color output to make the search string stand out a bit more.

Example: grep --color=auto John sample.log

To enable the color at all the time use "Export GREP_OPTIONS".

Example: export GREP_OPTIONS='--color=auto'

AWK: awk can be used extract just a list of specific things.

example: grep -i error sample.log | awk {'print $12'}

What print statement does is to count the 12th thing that it comes across line by line, space delimited and only return that value.What if you have to pull specific information out of the output of a command, like from a ping command you just want response time to be captured.

So to get where I want, I'll pipe the result of grep into a command called cut which lets you slice up lines based on a particular criteria. For this I'll use the d flag, which let's me specify a delimiter. In this case I'll use the equal sign, and I'll follow that up with dash f four, because I want field four.

example:
ping -c 1 example.com | grep 'bytes from' | cut -d = -f 4

1 comment:

  1. As stated by Stanford Medical, It's really the ONLY reason this country's women get to live 10 years longer and weigh 42 lbs lighter than us.

    (And actually, it is not about genetics or some secret exercise and absolutely EVERYTHING around "how" they are eating.)

    BTW, What I said is "HOW", not "WHAT"...

    CLICK this link to discover if this brief questionnaire can help you decipher your true weight loss possibility

    ReplyDelete