PHP: Check If String Is JSON

Body: 

A simple function to check if a string is valid JSON.

  1. function is_json($in) {
  2. json_decode($in);
  3. return (json_last_error() == JSON_ERROR_NONE);
  4. }
Language: 

Linux grep - returning matched patterns and inverting results.

How can I get grep to only return the portion of a string that matches a regular expression, and not the entire line of text?


--- A snippit from the grep man pages ---
-o
--only-matching
Show only the part of a matching line that matches PATTERN.


uptime | grep -o 'load average:.*$'

This will cause grep to only return the portion of the document that matches a given regular expression.

load average: 0.13, 0.15, 0.16

Using 'grep' how can I invert the returned results? In other words, how can I return lines that do not match a regular expression?

--- A snippit frmo the grep man pages ---
-v
--invert-match
Invert the sense of matching, to select non-matching lines.

The invert match switch causes grep to pipe out the lines of text that do not match the regular expression provided.

ls -lha | grep -v '^d'

This will return a directory listing of all lines that do not start with 'd'.

-rw-r--r--  1 josh josh  901 2012-10-07 23:23 awk
-rw-------  1 josh josh  73K 2012-10-10 12:51 .bash_history
-rw-r--r--  1 josh josh  220 2012-05-23 05:30 .bash_logout
-rw-r--r--  1 josh josh 3.1K 2012-10-08 23:31 .bashrc
Tags: 

Script the retrieval of USGS earthquake data for a given state.

This one-liner will snag any earthquakes in Hawaii that are of Magnitude 2.5 or greater from the last 30 days.

wget -qO- http://earthquake.usgs.gov/earthquakes/feed/csv/2.5/month | grep '[Hh]awaii'

Atom, GeoJSON and GeoJSONP feeds are also available at http://earthquake.usgs.gov/earthquakes/feed/

Tags: 

fail2ban: Fixing sasl.conf on Ubuntu

The default regex patterns used by fail2ban that locate failed login attempts do not work by default for Zimbra.

You will need to modify /etc/fail2ban/filters.d/sasl.conf

Specifically, you will need to modify the failregex line. Comment out, or replace the old line with the following.

failregex = \[<HOST>\]: SASL (?:LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) authentication failed

Breakdown Text File With AWK

Body: 

awk '{ print "\nLine", NR": "$0; for (i=1; i<=NF;++i) print "  Field", i": "$i }'

This short AWK script will help you understand the layout of a space separated file.

As an example, here is what the output may look like after piping in ls -lha

ls -lha | awk '{ print "\nLine", NR": "$0; for (i=1; i<=NF;++i) print "  Field", i": "$i }'

This will output something similar to the following output.

  1. Line 2: drwxr-xr-x 12 josh josh 4.0K 2012-09-07 22:58 play
  2. Field 1: drwxr-xr-x
  3. Field 2: 12
  4. Field 3: josh
  5. Field 4: josh
  6. Field 5: 4.0K
  7. Field 6: 2012-09-07
  8. Field 7: 22:58
  9. Field 8: play
Language: 

Top 10 Commands Used In Bash

Body: 

history | awk '{ print $2 }' | sort | uniq -c | sort -rn | head -10

This will list the top 10 most popular commands available in your Bash history.

Language: