Perl: Count occurrences of substring 2010-09-02 08:46am
I needed a quick way to count the number of times a substring appears in a larger string.
$count = @{[$haystack =~ /$needle/g]};
I needed a quick way to count the number of times a substring appears in a larger string.
$count = @{[$haystack =~ /$needle/g]};
Here is a pretty genius way of running a cron job on the last day of the month. It just checks that tomorrow is the first of the month, and runs your script if it is.
0 8 28-31 * * [ "$(/bin/date +%d -d tomorrow)" = "01" ] && /your/script.sh
I needed a pure bash way to get just the raw IP address from eth0
# Get the IP address line of eth0 from ifconfig
line=$(ifconfig eth0 | head -n2 | tail -n1)
# Regexp to match just the IP part
reg="addr:([0-9\.]+)\s"
# Run the regexp and print out the match
[[ $line =~ $reg ]]
echo ${BASH_REMATCH[1]}
Or the same thing on one line
line=$(ifconfig eth0 | head -n2 | tail -n1);reg="addr:([0-9\.]+)\s";[[ $line =~ $reg ]]; echo ${BASH_REMATCH[1]}
Or here is another way to do it using cut
# Cut on the spaces and find the 12th field (addr:1.2.3.4) then get the string starting at the 6th char
ifconfig eth0 | head -n2 | tail -n1 | cut -d' ' -f12 | cut -c 6-
There are four easy ways to list all the environment variables in bash.
env
set
export
declare -x
The simple env command is probably the most commonly used.
If you want to pull an environment variable into a bash script do
foo=$ENV_FOO;
To do the same, but assign foo a default value if there is no environment variable
foo=${ENV_FOO:-my_default_value}
Note: the - preceding the default value is required.
I had to setup a new user on a Linux server and I wanted to give them a temporary password and make them change it after their first login. You can use the chage command to control when a password expires (and requires a change). Create a new user and give them a default password. Then set that user's password to expire immediately.
chage -d0 username
This will set the password for username to expire in 0 days.
This is a pretty cool comparison of javascript trim() implementations.
I have several elements that I just want to pull the text() out of and put into an array.
var my_items = new Array();
my_items = $(".selector").map(function() { return $(this).text(); });
Or you can do it an alternate way:
var my_items = new Array();
$(".selector").each(function() { my_items.push($(this).text()); });
This command will find all your Pidgin (libpurple) log files that are more than 30 days old and remove them.
find ~/.purple/logs/ -type f -ctime +30 -exec rm {} \;
$* says to take all the command line arguments and put them together as one string. This is needed if you want to make a directory with spaces in the filename.
mkcd () {
mkdir -p "$*"
cd "$*"
}
Stolen from lifehacker.
I needed to run a cronjob to log some stats every 15 seconds. Cron only allows you run a command at minimum once a minute. This presents a problem, but you can cheat using sleep to get around it.
* * * * * ~/loggingscript.pl ; sleep 15 ; ~/loggingscript.pl ; sleep 15 ; ~/loggingscript.pl ; sleep 15 ; ~/loggingscript.pl
Total run time 64 minutes with a total of 53 sprinklers. The control unit is a Rain Bird ESP-8si.
This is a standard normal HTTP request
GET / HTTP/1.1
User-Agent: curl/7.20.1 (x86_64-redhat-linux-gnu) libcurl/7.20.1 NSS/3.12.6.2 zlib/1.2.3 libidn/1.16 libssh2/1.2.4
Host: www.google.com
Accept: */*
HTTP/1.1 200 OK
Date: Thu, 10 Jun 2010 17:22:54 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=5e8e80a9e66b65a2:TM=1276190574:LM=1276190574:S=UwbV5O8HJMVqISMc; expires=Sat, 09-Jun-2012 17:22:54 GMT; path=/; domain=.google.com
Server: gws
X-XSS-Protection: 1; mode=block
Proxy-Connection: close
This is a 404 error HTTP request
GET /thisisa404.html HTTP/1.1
User-Agent: curl/7.20.1 (x86_64-redhat-linux-gnu) libcurl/7.20.1 NSS/3.12.6.2 zlib/1.2.3 libidn/1.16 libssh2/1.2.4
Host: www.google.com
Accept: */*
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Thu, 10 Jun 2010 17:23:35 GMT
Server: sffe
Content-Length: 1369
X-XSS-Protection: 1; mode=block
Proxy-Connection: Keep-Alive
Today I was trying to figure out how a transistor works (with my Arduino) and I found a great explanation. The simple answer is that a transistor is an electronic switch. It has a voltage in (collector), a voltage out (emitter), and a control wire (base). The switch is normally open, but when you apply voltage to the control wire it completes the circuit. This allows you to supply a small amount of voltage (5v) to complete a circuit that's higher voltage (12v).
For some legacy code I needed a standard a JS object (i.e. not JQuery). You can use JQuery to perform the selection, and then .get() to give you the raw HTML Element Object.
$("#my_selector").get(0)
Note: you have to give .get() a index to return. Even if only one element is in the return list, .get() still returns a an array.