A simple English to Ubby Dubby translator.
echo Can you understand this? |sed -e ’s/\([aeiouAEIOU]\{1,\}\)/ub\1/g’
RSS | Comments RSS |
A simple English to Ubby Dubby translator.
echo Can you understand this? |sed -e ’s/\([aeiouAEIOU]\{1,\}\)/ub\1/g’
BASH: Split a string without ‘cut’ or ‘awk’
For a little test script I’m writing I needed to split a line on a ‘;’ but preservere the “s and ’s, something that echo doesn’t like to do. Digging deeper into the bash docs I see that there are some handy string handling functions.
#!/bin/bash
line=’this “is” a command;this “is” a pattern’
COMMAND=${line%;*}
PATTERN=${line#*;}
echo $COMMAND
echo $PATTERN
And the output would be:
this “is” a command
this “is” a pattern
BASH: Convert Unix Timestamp to a Date
I’ve been asked this a number of times and always have to look it up, so here are 3 ways to convert a unix timestamp (seconds since Jan 1, 1970 GMT) to a real looking date.
Perl method 1: use the ctime module:
perl -e “require ‘ctime.pl’; print &ctime($EPOCH);”
Perl method 2: use the scalar and localtime functions:
perl -e “print scalar(localtime($EPOCH))”
Awk has a wrapper for the standard C strftime function:
echo $EPOCH|awk ‘{print strftime(”%c”,$1)}’
Here’s a sample script that uses all methods.
!#/bin/bash
EPOCH=1000000000
DATE=$(perl -e “require ‘ctime.pl’; print &ctime($EPOCH);”)
echo $DATE
DATE=$(perl -e “print scalar(localtime($EPOCH))”)
echo $DATE
DATE=$(echo $EPOCH|awk ‘{print strftime(”%c”,$1)}’)
echo $DATE[update: Thanks to S. Maynard for reminding me of the proper use of quotes and how to avoid using the pipe…]
DATE=$(awk “BEGIN { print strftime(\”%c\”,$EPOCH) }”)
[UPDATE]
A reader found another way listed below. This doesn’t seem to be as portable (The mac ignores the –date and -d is an illegal option).
# date –date=’1970-01-01 1000000000 sec GMT’
Sat Sep 8 20:46:40 CDT 2001
[UPDATE]
# date -d @1000000042
Sun Sep 9 01:47:22 GMT 2001
But this only works on newer versions of date. It fails on my FC2 server and my Debian Sarge machine, but works fine on Ubuntu Feisty and Debian Etch.
Open Tech Support - [Unix/Linux] Linux Shell One-Liners
Open Tech Support - [Unix/Linux] Linux Shell One-Liners
A handy little list of some one liners. I use stuff like this every day and should really get in the habit of posting them.
A short introduction to getting a little more from your shell with piping & redirection
I just learned that you can use any character, not just the slash (/), for a delimitter when substituting with sed. Most unique characters should work.
This is especially useful when editing streams of file paths.
Here’s an example that modifies the path in a playlist:
cat list.m3u | sed ’s+/home/aolsen/+~/+’
The +’s worked well as a delimitter here and eliminate the need to quote the /’s
This also works in vi.
–Anton
I’m working on a script randomly grab mp3s from a playlist and fill a CD with shuffled songs.
I’ll be using bash’s built-in random number generator which I believe is available in all versions.
Here’s the shuffle function for those interested:
# Usage: shuffle <filename>
function shuffle () {
cat "${@}"|grep -v "^#" |
while read mp3 ; do
echo "$RANDOM $mp3"
done | sort | cut -d" " -f2-
}
Requirements will be mpg321 and cdrecord.
using a bash ‘for loop’ to wget
One of the ways that I frequently fetch files from the internet is with wget. It is a very useful command line utility that is capable of fetching anything from one file to mirroring whole sites.
When looking for new and interesting music I often find myself on a page with a few or more mp3 urls. Using firefox to download them all, even with a good download manager is tedious. The following bash script will read from stdin and download each url it sees.
# while read url ; do wget "${url}" ; done
I’m going to try to post a few howto articles for some of the simpler tasks. Many of these will be things that I use daily.
It is often necessary to read a file with bash, and act upon the entire line. There are many different ways to do this, but I’ll outline two of the simpler methods, both suitable for stacking on a single command line.
For this exercise I’ll assume the file is a list of files that we need to execute a command on.
# cat file.lst |while read line; do echo "${line}"; done /tmp/file1.txt /tmp/file with space.txt #
Final installment of the podcast bash script for a while. I will eventually add the ability to auto-sync with my iPod, but I’m still undecided at which command line iPod tools I want to use. Currently gtkpod is doing a great job of keeping my iPod fed, and it’s simple to load it up, add my podcast folder and sync. gtkpod weeds out the dupes and automatically transfers just what’s needed.
I’m just going to describe the tagmp3 function, much like I did for the updated getmp3 function last time. Read the script to see how I hook the tagmp3 function into getmp3. It could all us a bit more error handling, but it works well enough now to be useful.
I am using id3v2 from here. It is easy to compile and only has one dependency, id3lib. I’m using the id3lib and id3lib-devel RPMs for FC3 from FreshRPMs.
So far so good. The next exercise is to actually make this script useful by fetching the MP3s and arranging them into easy to understand directories.
The first order of business is to determine the name for the folder. I chose to use the channel name, and if that fails, the title of this episode. The later isn’t a very good choice as we could easily end up with a directory for each item in the feed.
| Designed by Kaushal Sheth | Based on Andreas02 | Modified by Anton Olsen |