User Tools

Site Tools


plaintext

Table of Contents

Plaintext

My Use

I mostly use MPD for playing digital music. This means the playlists are in a plaintext format (m3u), which I find really handy as I can easily modify them. Some examples:

Search playlists for a term, for example a song or artist name.

grep -r -n -i [term] .

Replace term in playlists. For example if I move a directory or change a filename. You have to escape more non alphanumeric characters with a \, common ones being /, [, ], (, ). You can replace the asterisk with a specific file if you want. I'm using a pound symbol instead of the deaful / to make this a little less of a hassle.

sed -i 's#[find]#[replace]#g' *

I have a convoluted Bash script to convert the paths in playlists formatted to work with MPD to ones that will works with, say, VLC. But hey, I learnt how to add arguments and do if/then statements in Bash so I'm very happy it works. (still have to add logging and show diff options)

#!/bin/bash
#	Converts .M3U playlists generated in MPD into
#	ones usable outside MPD—in VLC, for example—by
#	prepending information to the paths.
 
#	options:
#	-f	Convert single playlist, provide filename with extension.
#		By default it will convert all .M3U files in mpd directory
#	-a	Convert to absolute paths (in /home/[user]/Music/Playlists/path_absolute)
#	-r	Convert to relative paths (in /home/[user]/Music/Playlists)
 
dir=true
abs=false
rel=false
 
while getopts :f:ard flag
do
	case $flag in
		f)
			file=${OPTARG}
			dir=false
			;;
		a)
			abs=true
			;;
		r)
			rel=true
			;;
	esac
done
 
if [ "$dir" = true ]
then
	if [ "$abs" = true ] && [ "$rel" = false ]
	then
		for pl in /home/[user]/Music/Playlists/mpd/*.m3u
			do
				sed -e "s#^#/home/[user]/Music/#" "$pl" > "/home/[user]/Music/Playlists/path_absolute/${pl##*/}"
		done
		echo "Converted directory using absolute paths"
	elif [ "$rel" = true ] && [ "$abs" = false ]
	then
		for pl in /home/[user]/Music/Playlists/mpd/*.m3u
			do
				sed -e "s#^#../#" "$pl" > "/home/[user]/Music/Playlists/${pl##*/}"
		done
		echo "Converted directory using relative paths"
	elif [ "$abs" = true ] && [ "$rel" = true ]
	then
		for pl in /home/[user]/Music/Playlists/mpd/*.m3u
			do
				sed -e "s#^#/home/[user]/Music/#" "$pl" > "/home/[user]/Music/Playlists/path_absolute/${pl##*/}" &&
				sed -e "s#^#../#" "$pl" > "/home/[user]/Music/Playlists/${pl##*/}"
		done
		echo "Converted directory using absolute and relative paths"
	fi
elif [ "$dir" = false ]
then
	if [ "$abs" = true ] && [ "$rel" = false ]
	then
		sed -e "s#^#/home/[user]/Music/#" "/home/[user]/Music/Playlists/mpd/$file.m3u" > "/home/[user]/Playlists/path_absolute/$file.m3u"
		echo "Converted single file using absolute paths"
	elif [ "$rel" = true ] && [ "$abs" = false ]
	then
		sed -e "s#^#../#" "/home/[user]/Music/Playlists/mpd/$file.m3u" > "/home/[user]/Music/Playlists/$file.m3u"
		echo "Converted single file using relative paths"
	elif [ "$abs" = true ] && [ "$rel" = true ]
	then
		sed -e "s#^#/home/[user]/Music/#" "/home/[user]/Music/Playlists/mpd/$file.m3u" > "/home/[user]/Playlists/path_absolute/$file.m3u" &&
		sed -e "s#^#../#" "/home/[user]/Music/Playlists/mpd/$file.m3u" > "/home/[user]/Music/Playlists/$file.m3u"
		echo "Converted single file using absolute and relative paths"
	fi
fi

Metaforms

Tools

Grep

See Also

plaintext.txt · Last modified: 2024/03/02 18:03 by rjt