You are not logged in.

#776 2010-01-13 03:45:44

upsidaisium
Member
From: Vietnam
Registered: 2006-09-16
Posts: 263
Website

Re: Post your handy self made command line utilities

any sed/regex gurus out there? i made a little script to pull the word of the day from dictionary.com's rss feed:

#!/bin/sh

URL=http://feeds.reference.com/DictionarycomWordOfTheDay
w3m -dump $URL | grep 'description.[[:alpha:]]*:' | sed 's/.*description.\(.*\)<.*/\1/'

which works fine (todays word: torpor: lethargic indifference; apathy. fyi). but i know only slightly more about using sed and regular expressions than, say.. a chimp. so i can't help but think that i could do something more simple/elegant to pull the relevant sentence from the rss feed. my thought process was: i) grep for the relevant line; ii) do some sed magic to get just the right sentence from that line, because it also has other junk in it. and then i tinkered with it, tried to get rid of the grep call and do it all in sed---with less than stellar results. so i've left it like this (so it will actually work!), but i'm wondering how else it could be accomplished? hopefully somebody here can teach me a new trick smile

edit: just realized i could pull the feed with w3m rather than wget

Last edited by upsidaisium (2010-01-13 03:51:43)


I've seen young people waste their time reading books about sensitive vampires. It's kinda sad. But you say it's not the end of the world... Well, maybe it is!

Offline

#777 2010-01-14 02:32:21

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Post your handy self made command line utilities

Script to generate a list of updates in format 'name current_version -> new_version'.  Suitable for embedding in conky or some such.

#!/bin/bash
nu=0
pacman -Qu | while read name ver; do
  let ++nu
  test $nu -gt "$1" && break

  newver="`pacman -Si "$name" | sed -n 's/Version\s*:\s*\(.*\)$/\1/p'`"
  echo "$name $ver -> $newver"
done
test "`pacman -Qu | wc -l`" -gt "$1" && echo '...'

The first argument is the number of entries you want to display. If it's less than the total number of updates, an ellipsis will be appended.

Note: the script is abysmally slow, so you'll probably want to ${execi 60 ...} it. Could probably speed it up, but my regex-fu was rusty.

Last edited by Peasantoid (2010-01-14 02:40:11)

Offline

#778 2010-01-16 00:17:15

Ghost1227
Forum Fellow
From: Omaha, NE, USA
Registered: 2008-04-21
Posts: 1,422
Website

Re: Post your handy self made command line utilities


.:[My Blog] || [My GitHub]:.

Offline

#779 2010-01-16 00:42:46

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

I still feel a shell-fu style site would be superior. I laid out some brainstorming somewhere earlier in the thread. Patches welcome big_smile

Edit:

Daenyth wrote:

Something like shell-fu would be best here... Sounds like a great project for someone to make!

It should have sorting/listing and searching based on tags, script name, and a brief description. It should have language-aware syntax highlighting, which would hopefully try to guess (asking the user to confirm language). It should have a discussion area for someone to submit comments and suggestions for the script. The script should be editable by the user who submitted it, with a versioned history (I'm thinking like the mediawiki page history listing). User comments should be tagged somehow with the version they commented against, so that it's clear what they are referring to. It should also have a spot for the author to provide notes or a longer description.

So, who's going to make it? tongue

Last edited by Daenyth (2010-01-16 00:46:38)

Offline

#780 2010-01-16 00:51:22

Ghost1227
Forum Fellow
From: Omaha, NE, USA
Registered: 2008-04-21
Posts: 1,422
Website

Re: Post your handy self made command line utilities

Hmm.... I'll see what I can come up with tongue


.:[My Blog] || [My GitHub]:.

Offline

#781 2010-01-16 00:54:54

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

You are just too cool <3

For the tagging, maybe try to add an alias feature. For example, if you put "mtg" as a game tag in your profile on NearbyGamer.net, it automatically sets the tag as "Magic: The Gathering". Might help tag overlap issues.

Offline

#782 2010-01-17 06:22:28

Kitty
Member
From: The Burning Desert
Registered: 2008-01-11
Posts: 88

Re: Post your handy self made command line utilities

upsidaisium wrote:

any sed/regex gurus out there? i made a little script to pull the word of the day from dictionary.com's rss feed:

How does this work for you:

wget -q -O - http://feeds.reference.com/DictionarycomWordOfTheDay | sed '/description.*&lt/!d; s/.*>\(.*\)&lt.*/\1/'

/etc/rc.d/ is where daemons reside. Beware.

Offline

#783 2010-01-17 15:44:08

Hiato
Member
Registered: 2009-01-21
Posts: 76

Re: Post your handy self made command line utilities

Strange seeing this here, now ...

I had the same thing set up for a while, but my regex sucks even more than previously thought possible. Mine goes like this:

#! /bin/sh
if [ ! -f /tmp/wotd ]; then
        wget http://feeds.reference.com/DictionarycomWordOfTheDay --quiet -O - | grep '^.*\<description\>.[[:alpha:]]*\:' | sed 's/\(.*\)>//' | sed 's/\&\(.*$\)//' > /tmp/wotd
fi;
cat /tmp/wotd

You gotta save bandwidth these days tongue

Last edited by Hiato (2010-01-17 15:45:15)

Offline

#784 2010-01-18 11:52:38

upsidaisium
Member
From: Vietnam
Registered: 2006-09-16
Posts: 263
Website

Re: Post your handy self made command line utilities

Kitty wrote:
upsidaisium wrote:

any sed/regex gurus out there? i made a little script to pull
the word of the day from dictionary.com's rss feed:

How does this work for you:

wget -q -O - http://feeds.reference.com/DictionarycomWordOfTheDay | sed '/description.*&lt/!d; s/.*>\(.*\)&lt.*/\1/'

works like a charm! so, let's see if i understand.. /description.*&lt/!d; deletes
everything but the line that matches that regex?? and then the second part finds the word of the
day (+ definition) in the line that wasn't deleted? seems to make sense! and most importantly,
it works smile


I've seen young people waste their time reading books about sensitive vampires. It's kinda sad. But you say it's not the end of the world... Well, maybe it is!

Offline

#785 2010-01-18 16:54:42

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: Post your handy self made command line utilities

Literally whipped this up in 3 minutes, but I find it quite useful. It's for organizing images.

#!/bin/zsh

case $1 in
    -add)
        mkdir -p ~/.picdb/$2
        mv $*[3,-1] ~/.picdb/$2 ;;
    -get)    
        find ~/.picdb/$2 -type f 2>/dev/null || {
            print "No pictures with that tag" >&2
            return 1
        } ;;
    *)
        print "Usage: $(basename $0) -add [TAG] [PICS] or -get [TAG]"
        return ;;
esac

Some examples:

View all images that match a tag name:

imageviewer $(picdb -get tagname)

Shuffle between wallpapers:
Add some pictures to "wallpapers" with picdb -add wallpapers picshere, then add this to .xinitrc:

while true; do
    feh --bg-center "$(picdb -get wallpapers | shuf -n 1)"
    sleep 15m
done &

edit: fixed bug
edit2: improvement

Last edited by JohannesSM64 (2010-01-22 12:48:01)

Offline

#786 2010-01-18 16:56:23

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: Post your handy self made command line utilities

^
|

This + Openbox = win

Offline

#787 2010-01-19 05:42:41

res
Member
Registered: 2010-01-14
Posts: 55

Re: Post your handy self made command line utilities

Local absget with completion. Works with globs and more than one search term:

absget() {
  local repo
  [[ $1 == -r ]] && { repo=$2; shift 2; } || repo=@(community|core|extra)

  until (( $# == 0 )); do
    find /var/abs/$repo -name "$1" -type d -exec cp -r {} . \;
    shift
  done
}

_absget() {
  local cur=`_get_cword`
  COMPREPLY=($(compgen -W "$(if [[ ${COMP_WORDS[1]} == '-r' ]]; then
                               command pacman -Slq "${COMP_WORDS[2]}"
                             else
                               command pacman -Slq
                             fi)" -- "$cur"))
}

complete -F _absget absget

e: group that shift 2 ;)

Last edited by res (2010-01-19 18:04:44)

Offline

#788 2010-01-19 06:48:59

urist
Member
Registered: 2009-02-22
Posts: 248

Re: Post your handy self made command line utilities

This solves a very specific problem. My web server in my uni absolutely must connect through wifi. It doesn't get much traffic, so it frequently gets kicked off the network.

#!/bin/bash
while true
do
     sleep 10m
     curl google.com &> /dev/null
done

Why not use ping? It's filtered, just like at every other learning institution I've been to. It's not a cry out for help. It does its job with what is probably the least wasted bandwidth possible. Just

sh keepalive.sh &

and forget. Yes, it's trivial, but I find it useful.

Update: Didn't work. I suspect other problems with my university.

Last edited by urist (2010-01-19 14:15:59)

Offline

#789 2010-01-19 10:37:15

dennis123123
Member
Registered: 2009-07-02
Posts: 72

Re: Post your handy self made command line utilities

To save resources, may I suggest a cron entry for "curl google.com" instead of a separate script?
I only use scripts for occurances less than 1 minute (which cron cannot manage)

Offline

#790 2010-01-19 12:37:19

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

I think you can give curl a range so that you could tell it to only fetch the first few bytes.

Offline

#791 2010-01-19 13:07:10

samjh
Member
From: Australia
Registered: 2009-08-25
Posts: 34
Website

Re: Post your handy self made command line utilities

Here's mine for calculating the correct font dpi for your screen resolution and size.

Example usage: ./dpi 1280 1024 19
Where: 1280 is the horizontal resolution, 1024 is the vertical resolution, and 19 is the size of the screen in inches (measured from one corner of the screen to the opposite diagonal corner).

Disclaimer: The script depends on your inputs and may not yield the most optimal result!  For best results, use the native resolution of your screen (consult the technical manual) and actual viewable area of the screen.  Keep in mind that 72dpi and 96dpi are the two standard dpi sizes fonts are typically designed for.

#! /usr/bin/env python

import sys, math

def calculatedpi():
    hres = float(sys.argv[1])
    vres = float(sys.argv[2])
    dsize = float(sys.argv[3])
    dpi = hres / (hres / math.sqrt(hres**2 + vres**2) * dsize)
    print "Your screen's dpi =", int(dpi)
    print ""

def main():
    argerror = False
    if len(sys.argv) < 4:
        argerror = True
    else:
        for arg in sys.argv[1:]:
            if arg.isdigit() == False:
                argerror = True
    if argerror == True:
        print "DPI calculator"
        print "Usage: dpi hres vres size"
        print "Where:"
        print "  hres = horizontal resolution in pixels"
        print "  vres = vertical resolution in pixels"
        print "  size = size of screen in inches"
        print "Example:"
        print "  dpi 1280 1024 19"
        print "  Your screen's dpi = 86"
        print ""
    else:
        calculatedpi()

if __name__ == "__main__":
    main()

Last edited by samjh (2010-01-19 23:17:48)

Offline

#792 2010-01-19 16:17:39

kourosh
Member
From: England
Registered: 2009-03-10
Posts: 241
Website

Re: Post your handy self made command line utilities

Interesting script samjh, tells me my proper DPI is 86...maybe thats why my fonts aren't great as I use 96 DPI...

Offline

#793 2010-01-19 16:27:51

Meyithi
Member
From: Wirral, UK
Registered: 2009-06-21
Posts: 550
Website

Re: Post your handy self made command line utilities

kourosh wrote:

Interesting script samjh, tells me my proper DPI is 86...maybe thats why my fonts aren't great as I use 96 DPI...

Be careful with it, told me that my 26" Bravia @ 1360x768 was 60dpi - tried to configure my stuff as such but was an absolute nightmare, gave up and went back to 96.


The mind roams more freely in empty rooms.
dwm - colours - ncmpcpp - system
irc://irc.freenode.net:meyithi

Offline

#794 2010-01-19 16:34:48

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

What if it's a laptop screen?  Eg. 13.3" or 12.1" etc

Offline

#795 2010-01-19 16:53:38

kourosh
Member
From: England
Registered: 2009-03-10
Posts: 241
Website

Re: Post your handy self made command line utilities

Meyithi wrote:
kourosh wrote:

Interesting script samjh, tells me my proper DPI is 86...maybe thats why my fonts aren't great as I use 96 DPI...

Be careful with it, told me that my 26" Bravia @ 1360x768 was 60dpi - tried to configure my stuff as such but was an absolute nightmare, gave up and went back to 96.

Hmm, just worked out to see if xorg recognises my dimensions correctly..bit of pythagoras later and it says I have a 17" screen...I have a 19" screen...still running on 96 dpi for now but I might give 86 a go. Is xorg detecting my screen size wrong?

Sorry, bit OT but the script itself seems to work out correctly.

Offline

#796 2010-01-19 17:27:00

jdarnold
Member
From: Medford MA USA
Registered: 2009-12-15
Posts: 485
Website

Re: Post your handy self made command line utilities

Display a random 15 mp3 files:

$ find ~/Music -name "*.mp3" | shuf -n 15

I use this because my music collection contains many songs in there twice, one MP3 & one FLAC, depending on what I want to do with it. Ahh, the joys of a 1.5TB hard drive smile And I've yet to find a music player that treats a song with all the same tags, and just a different extension, as the same song.

Offline

#797 2010-01-19 23:13:35

samjh
Member
From: Australia
Registered: 2009-08-25
Posts: 34
Website

Re: Post your handy self made command line utilities

steve___ wrote:

What if it's a laptop screen?  Eg. 13.3" or 12.1" etc

I hadn't considered that possibility! sad

The script gives the mathematically correct dpi for a user-provided specification, but this may not actually be the most optimal.  Each screen has its native resolution and viewable area (not necessarily the same as the physical monitor size, although it should be the same for most LCD monitors), and the dpi should actually be calculated using those values.

Also, there are two standard dpi values: 72dpi, which is used for Mac monitors and is the typographical standard (the size of a "point" used in publishing most closely match the pixel size of those monitors, in theory); and 96dpi, which is what is used by Windows (typical of Microsoft to come up with their own).  Fonts tend to be designed around those two standards.

I got the idea of making a script like it when I was distro-hopping between Ubuntu and Fedora, and realised that the two distros were using different dpi settings.  Then I started using Xfce and discovered that Xorg sometimes detects the wrong dpi and makes fonts look extremely small.  My screen's correct physical dpi is 86dpi (1280x1024 native resolution and 19" viewable area), which is calculated correctly by my script.

Offline

#798 2010-01-19 23:37:36

kourosh
Member
From: England
Registered: 2009-03-10
Posts: 241
Website

Re: Post your handy self made command line utilities

Xorg apparently won't let me override DPI, but since Windows uses 96 DPI, and its autodetected 96, I'm going to go with that. I've probably got a monitor manual about but I've lost it. Nice script though.

Offline

#799 2010-01-22 11:55:49

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 788
Website

Re: Post your handy self made command line utilities

Simple BASH function for doing quick decimal/hexadecimal color conversions:

function color {
    if [[ $1 =~ ([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2}) ]]
    then
        printf "(%d, %d, %d)\n" \
               0x${BASH_REMATCH[1]} 0x${BASH_REMATCH[2]} 0x${BASH_REMATCH[3]}
    elif [[ $1 =~ ([[:digit:]]{1,3}),([[:digit:]]{1,3}),([[:digit:]]{1,3}) ]]
    then
        printf "#%02x%02x%02x\n" ${BASH_REMATCH[1]} ${BASH_REMATCH[2]} ${BASH_REMATCH[3]}
    fi
}

Usage:

$ color 0,255,127
#00ff7f
$ color 00ff7f
(0, 255, 127)

Last edited by mikesd (2010-01-22 11:58:01)

Offline

#800 2010-01-22 21:58:25

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Post your handy self made command line utilities

jdarnold wrote:

Display a random 15 mp3 files:

$ find ~/Music -name "*.mp3" | shuf -n 15

I use this because my music collection contains many songs in there twice, one MP3 & one FLAC, depending on what I want to do with it. Ahh, the joys of a 1.5TB hard drive smile And I've yet to find a music player that treats a song with all the same tags, and just a different extension, as the same song.

thanks, i took this and expanded on it abit smile


you use this like :

randommp3.sh 5

where 5 is the number of random songs you want to play.

#!/bin/bash 
find ~/torrents/music/ -name "*.mp3" | shuf -n $1 > .mp3s
mpg123 -@ .mp3s
rm .mp3s

Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

Board footer

Powered by FluxBB