Freitag, 25. März 2011

Most recent marks

Problem We have two files with the grades of the students. One file representing the first exam -  marks1.txt - the second file the second exam (for mending ones mark - thus overwriting the first mark) - marks2.txt.

The second exam though is optional.

The exam file syntax is:


For sorting we have to be able to identify from which file the entry stems from - so we prepend a "file-id" column - with value 2 for 1st exam and 1 for 2nd exam.

We sort now both files, firstly on the student-id (column 2) and secondly on the file-id (column 1).

i.e.:
marks1.txt:
012  3             -->   2 012 3


marks2.txt
012  4            -->    1 012 4

sort -k 2,2 -k 1,1 
       <(
         sed -e 's/^/2 /' marks1.txt
        ) 
 
       <(
         sed -e 's/^/1 /' marks2.txt
        )

After sorting we obtain:
1 012 4
2 012 3

If we have duplicates we only want to retain the first line (the most recent mark).
So lets first remove the file-id column again with a
cut --delimiter=' ' --fields=2- 

Then remove duplicates, checking on the student-id column (1st column 3 bytes wide):
uniq -w 3

Finally, we wan to link the student-ids to the student's names.
Let's say we have a file id_names.txt with the format:


All we have to do is make a join the student-id column.

The whole code:
join 
  <(
    sort -k 2,2 -k 1,1 
       <(
         sed -e 's/^/2 /' marks1.txt
        ) 
 
       <(
         sed -e 's/^/1 /' marks2.txt
        )
    |
     cut --delimiter=' ' --fields=2- 
    |  
     uniq -w 3
   )

  <(
    sort id_names.txt
   )

  > marks.txt
d

Donnerstag, 24. März 2011

online syntax highlighting

For generating html-code for syntax hightlighting of code I use an online service.

For the shell scripts I use http://tohtml.com/shell/ with style navy

join caveat

The join command requires the key fields to be sorted in ascending order, otherwise it won't join them:

join <( <file_A> ) <( <file_B> )  

Freitag, 24. September 2010

For drilling my listening comprehension skills in Russian I use audio news where they provide a reasonable faithful transcript (so far I haven't found a site, where they provided a 1:1 transcript).
In addition the news anchormen often speak a very fast Russian, so to be even able to understand it initially I have to slow the tempo of the audio down, but for that I have to download it:

On http://www.ntv.ru/novosti you can download videos
+ the transcription are supplied


The downloaded video is in the mp4 format
2 options now
1a) convert with mplayer into wav-format:
   mplayer -ao pcm:file="out.wav" "in.mp4"

1b) convert with ffmpeg directly into mp3-format:
   ffmpeg -i "in.mp4" -acodec libmp3lame -ab 128 "out.mp3"

2) If tempo is too fast to comprehend, slow the tempo with soundstretch (pitch won't be changed!):
   soundstretch "in.wav" "out.mp3" -tempo=-20




That slows down the tempo 20%.
Soundstretch needs a wave-file as input!

Enable seeking with hotkeys for xmms2

Intro

I study foreign languages with audio-books and transcripts. If I haven't understood a word I seek back a few seconds in the audio - manually until now by dragging the button on the seek bar of my favorite soundplayer gui.
This is of course tedious if your reading the transcript and have to refocus your eyes from the text to the seek bar and even seek back the correct amount of time.
I needed something more apt.

Specification
I'm running Debian 5 with Gnome and wanted
  • soundplayer with cli interface and capable of sending the following commands:
    • seek + amount of seconds to seek
    • toggle between play and stop
    • forward / backward track
  • bind the commands to the multimedia keys of my keyboard
Solution

I found the best solution being xmms2 as the soundplayer and xbindkeys for the key binding of the commands.


xmms2


xmms2 comes in a client/server architecture.
To start the sever in daemon mode invoke 'xmms2-launcher', but it can't be started as root.
A rudimentary gui for xmms2 is gxmms2.

Both xmms2 and xbindkeys have to be started each time you login, so I put them as autostart programs in 'System->Session->Autostart programs'.

xbindkeys


At first I had problems binding the multimedia keys, but then I read here that Debians/Ubuntus graphical keyboard shortcuts utility causes to suppress the keys of sending keycodes, as can be seen with the command 'xev'.
Solution: Remove the binding for these keys with the graphical shortcut utility, afterwards the keycodes show up.

A great tutorial on xbindkeys can be found here, for use of mouse, sending key combinations to programs (paket xautomation, program xte), graphical/sound responses

My .xbindkeysrc looks as follows (I use a Cherry G230 keyboard):

#Toggle between play/pause
"xmms2 toggleplay"
m:0x0 + c:162

#Seek +5 seconds
"xmms2 seek +5"
m:0x0 + c:153

#Seek -5 seconds
"xmms2 seek -5"
m:0x0 + c:144

Donnerstag, 20. Mai 2010

Link Tracklist metadata to id3-tags

Problem: You have a mp3 album, but the files have no id3-tags set, but you've found a tracklist with the format:

<track-number>. <artist> - <title>

i.e.:

cat tracklist.txt | 
 while read f; do
  tr=$(echo "$f" | sed -e 's/\. .*//');
  a= $(echo "$f" | sed -e 's/...\. \(.*\) - .*/\1/');
  t=$(echo "$f" | sed -e 's/.* - \(.*\)/\1/');
  mid3v2 -T "$tr" -a "$a" -t "$t" -A "100 хитов русского рока" -g 17
    "$(find ./ -type f -iname "${tr}-*")";
 done

We pipe the tracklist 'tracklist.txt' to a loop, which does for each line (mp3-file):
We extract the track# 'tr', the author 'a' and the title 't'.
Then we set the id3v2.4 tags with mutagen mid3vs of the corresponding file - identified by the track#, we get the filename with a find-cmd.
Additionally we set the Album (-A) to '100 хитов русского рока' (100 Russian rock hits).

convmv - Convert encoding of filenames.

convmv is a perl script, which converts filenames from one encoding to another

i.e.:
$ convmv -f iso8859-15 -t utf-8 --notest *mp3

converts all mp3-files in the current directory  from iso8859-15 to utf-8.
--notest is needed to actually convert the filenames, otherwise convmv does a dry-run and only prints what it would do.

You can even convert a hole filetree (recursively) by adding the -r otpion.

It also checks if the encoding is already in utf-8 and aborts.