• Willkommen im Linux Club - dem deutschsprachigen Supportforum für GNU/Linux. Registriere dich kostenlos, um alle Inhalte zu sehen und Fragen zu stellen.

Ab einer bestimmten Stelle abschneiden

Hallo Leute




Über Sinn oder Unsinn meiner Bastlereien lässt sich sicher streiten :)

Dennoch habe ich ein bestimmtes Anliegen wo ich im Moment nicht weiter weis.

Bin gerade an einem Script am basteln , das natürlich alles andere als fertig ist. es sind erstmal einfach nur Tests wo ich bestimmte Bausteine isoliert teste.
Code:
#!/bin/bash


#Beschreibung:
#Filtert auf http://www.lastfm.de/music/+free/ nach verwertbaren Links für den Lastfm-Player
#bzw schneidet diese entsprechend zurecht.


for musik in `dog --links http://www.lastfm.de/music/+free/ | grep 'autostart$' | \
sed  's/?autostart//g' | sed 's/music\/+free\///g'`
do
	echo "=========================================================="
	echo "		${musik}"
	dog --links ${musik} | sort -u | grep 'lastfm://play/tracks/'
done
Jetzt habe ich aber gemerkt das als Ergebnis noch nicht die verwertbaren Links rauskommen die ich brauche.


NUN ZU MEINER FRAGE:

Ich habe nun Links in unregelmäßiger Länge , die man also nicht exakt ab Stelle cut -c machen kann.
Auch fehlen ab hier die sich wiederholenden Ausdrücke die man in einem Rutsch mit sed entfernen könnte.

Beispiele:
=============================
http://www.lastfm.de/music/The+Last+Dance/_/Nightmares
http://www.lastfm.de/music/The+Mary+Onettes/_/Lost
http://www.lastfm.de/music/Mika/_/Romana
usw ...
=============================

daraus soll werden:
=============================
http://www.lastfm.de/music/The+Last+Dance/
http://www.lastfm.de/music/The+Mary+Onettes/
http://www.lastfm.de/music/Mika/
usw ...
=============================

Wie kann ich nun alles ab dem Unterstrich abtrennen , nachdem die Stelle ja immer wieder an einer anderen Stelle der Zeichenkette ist?

Wäre sehr dankbar um Antworten, da dieses Script mit der Lösung dieses Problems steht und fällt.




Gruß Peter
 
OP
T

trommelpeter

Member
Problem gelöst:

Code:
#!/bin/bash


#Beschreibung:
#Filtert auf http://www.lastfm.de/music/+free/ nach verwertbaren Links für den Lastfm-Player
#bzw schneidet diese entsprechend zurecht.


for musik in `dog --links http://www.lastfm.de/music/+free/ | grep 'autostart$' | \
sed  's/?autostart//g' | sed 's/music\/+free\///g' | sed 's/\/_\/.*//g'`
do
	echo "=========================================================="
	echo "		${musik}"
	dog --links ${musik} | sort -u | grep 'lastfm://play/tracks/'
done

Die Lösung ist:
Code:
sed 's/\/_\/.*//g'
Die Erleuchtung kam mir als mir einfiel das ich irgendwo was gelesen hatte. Ich musste die Stelle wo ich es gelesen habe nur wieder finden.

Also, dieser Baustein funktioniert. Jetzt kann das eigentliche Script basteln beginnen.

Gruß Peter

P.S. Wie markiert man einen Thread als gelöst?
 
Versuch es mal mit ${a%/*} um in der Variablen a alles hinter dem letzten / abzuschneiden.

me@home:>a=http://www.lastfm.de/music/The+Mary+Onettes/_/Lost
me@home:>echo ${a%/*}
http://www.lastfm.de/music/The+Mary+Onettes/_

Haveaniceday

Edit: gelöst müsste mit einem Edit im ersten Posting gehen.
 
OP
T

trommelpeter

Member
Versuch es mal mit ${a%/*} um in der Variablen a alles hinter dem letzten / abzuschneiden.

sed 's/\/_\/.*//g'

Hallo haveaniceday

Doch noch nicht gelöst :) Jetzt wirds erst interessant.
Jetzt bin ich neugierig geworden, was deine Lösung von meiner unterscheidet bzw worin evtl der Vorteil liegt.

Deine Lösung sieht auch sehr interessant aus.
Kannst du mir bisschen was dazu erklären?

Gruß Peter
 
Bei Bash gibt es schöne eingebaute Funktionen fürs ersetzen.
Und im Beispiel ist "word" */ ${parameter%word}

Code:
Change "extention" of a variable:

shell# a=replace/etc/replace/passwd.replace ; b=${a//replace/by} ; echo $b
by/etc/by/passwd.by

shell# a=replace/etc/replace/passwd.replace ; b=${a//#replace/by} ; echo $b
by/etc/replace/passwd.replace

shell# a=replace/etc/replace/passwd.replace ; b=${a//%replace/by} ; echo $b
replace/etc/replace/passwd.by

shell# a=/part1/part2/part3/part4.part5 ; b=${a##*/} ; echo $b
part4.part5

shell# a=/part1/part2/part3/part4.part5 ; b=${a#*/} ; echo $b
part1/part2/part3/part4.part5

shell# a=part1.part2 ; b=${a%.part2} ; echo $b
part1

shell# parameter=123456 ; echo ${parameter:1:3}
234


Code:
       ${parameter#word}
       ${parameter##word}
              The word is expanded to produce a pattern just as in pathname expansion.  If the pattern matches
              the  beginning of the value of parameter, then the result of the expansion is the expanded value
              of parameter with the shortest matching pattern (the ``#'' case) or the longest matching pattern
              (the  ``##'' case) deleted.  If parameter is @ or *, the pattern removal operation is applied to
              each positional parameter in turn, and the expansion is the resultant list.  If parameter is  an
              array  variable subscripted with @ or *, the pattern removal operation is applied to each member
              of the array in turn, and the expansion is the resultant list.

       ${parameter%word}
       ${parameter%%word}
              The word is expanded to produce a pattern just as in pathname expansion.  If the pattern matches
              a  trailing  portion of the expanded value of parameter, then the result of the expansion is the
              expanded value of parameter with the shortest matching pattern (the ``%'' case) or  the  longest
              matching  pattern (the ``%%'' case) deleted.  If parameter is @ or *, the pattern removal opera-
              tion is applied to each positional parameter in turn, and the expansion is the  resultant  list.
              If  parameter  is  an  array  variable subscripted with @ or *, the pattern removal operation is
              applied to each member of the array in turn, and the expansion is the resultant list.

       ${parameter/pattern/string}
              The pattern is expanded to produce a pattern  just  as  in  pathname  expansion.   Parameter  is
              expanded  and  the longest match of pattern against its value is replaced with string.  If Ipat-
              tern begins with /, all matches of pattern are replaced with string.  Normally  only  the  first
              match  is  replaced.   If  pattern begins with #, it must match at the beginning of the expanded
              value of parameter.  If pattern begins with %, it must match at the end of the expanded value of
              parameter.  If string is null, matches of pattern are deleted and the / following pattern may be
              omitted.  If parameter is @ or *, the substitution  operation  is  applied  to  each  positional
              parameter  in  turn, and the expansion is the resultant list.  If parameter is an array variable
              subscripted with @ or *, the substitution operation is applied to each member of  the  array  in
              turn, and the expansion is the resultant list.
 
OP
T

trommelpeter

Member
Bei Bash gibt es schöne eingebaute Funktionen fürs ersetzen.
Und im Beispiel ist "word" */ ${parameter%word}

Hallo haveaniceday

Bin im Moment bisschen überfordert mit deinem Beispiel. Ich fürchte aber das ich mich in ein Paar Grundbegriffe nochmal verstärkt einlesen muss.

Aber ich war heute schon sehr fleißg und habe ein echt abgefahrenes Script gebastelt.

Es ist aber im Moment noch nicht sehr Anwenderfreundlich.
Ich nenne es mal erster Entwurf 8)
:wink:

Code:
#!/bin/bash
set -x



#Beschreibung:
#Filtert auf http://www.lastfm.de/music/+free/ nach verwertbaren Links für den Lastfm-Player
#bzw schneidet diese entsprechend zurecht.

echo "Merke 1 ist immer quit"

ziel=/tmp/LastfmArtist
[ -d ${ziel} ] || mkdir ${ziel}



musikfinden () {
for musik in `dog --links http://www.lastfm.de/music/+free/ | grep 'autostart$' | \
sed 's/?autostart//g' | sed 's/music\/+free\///g' | sed 's/\/_\/.*//g'`
do
	artist=`echo ${musik} | cut -c 27- | sed 's/\///g' | sed 's/+/_/g'`
	dog --links ${musik} | sort -u | grep 'lastfm://play/tracks/' > ${ziel}/${artist}.txt
done
}




auswaelen () {
echo "zum Auswählen einfach Taste enter betätigen"
echo "mit der Zahl die für q steht kommt man aus der select-Schleife raus"
select artist in quit `find ${ziel}` 
do
	case ${artist} in
		quit)	break
	;;
		*)
			for lied in `cat ${artist}` 
			do
				/usr/bin/last.fm ${lied} &
				echo "Auswählen?"
				read antwort
				if [ ! ${antwort} ] ; then
					lied=`echo "${lied}" | cut -c 22-`
					echo -n "${lied}," >> /tmp/FreeDownloadCharts.txt
				fi	
			done		
	;;
	esac
done
if [ -f /tmp/FreeDownloadCharts.txt ] ; then
	mv /tmp/FreeDownloadCharts.txt ~/.FreeDownloadCharts.txt
else
	echo "Letzte Auswahl wird gespielt, da keine neue Auswahl vorhanden"
fi
}




abspielen () {
/usr/bin/last.fm lastfm://play/tracks/`cat ~/.FreeDownloadCharts.txt`
}


select funktion in quit musikfinden auswaelen abspielen 
do
	case ${funktion} in
		abspielen)
			if [ -f ~/.FreeDownloadCharts.txt ] ; then
				abspielen
			else
				echo "Keine Auswahl vorhanden" 
				echo "Bitte Funktion auswaehlen starten"
				exit
			fi
		;;
		auswaelen)
			a=`ls ${ziel}`
			if [ ! ${a} ] ; then
				echo "Keine Artist vorhanden"
				echo "bitte Funktion musikfinden starten"
				exit
			else
				auswaelen
				abspielen
			fi
		;;
		musikfinden)
			musikfinden
			auswaelen
			abspielen &
		;;
		quit)	exit
		;;
	esac
done

exit



set +x

Gruß Peter
 
OP
T

trommelpeter

Member
Hallo

Das Endergebnis um das es mir letztlich ging ist so gut wie fertig:
Siehe Hier:
http://www.linux-club.de/viewtopic.php?p=543372#543372

Gruß Peter
 
Oben