La chaîne est configuré comme ceci:Comment extraire une partie d'une chaîne via un script shell?
href="
PARTIE JE VEUX EXTRACT">[link]
La chaîne est configuré comme ceci:Comment extraire une partie d'une chaîne via un script shell?
href="
PARTIE JE VEUX EXTRACT">[link]
grep -o "PART I WANT TO EXTRACT" foo
Edit: "PARTIE JE VEUX Extract" peut être une expression rationnelle, à savoir :
grep -o http://[a-z/.]* foo
J'utilise: > 'grep -o 'http: // [az/.A-Z0-9] * " > \ [link \]'' mais il me donne: > 'http: // img18.imageshack.us/img18/8079/69903601.jpg " > [link] ' Je ne veux pas les choses à la fin. – Josh
Eh bien, vous pourriez grep à trouver, puis utilisez grep -o pour obtenir l'expression:> grep 'http: // [a-z/.A-Z0-9] * " > \ [link \]' | grep -o 'http: // [a-z/.A-Z0-9] * – ctd
expr "$string" : 'href="\(.*\)">\[link\]'
utilisation awk
$ echo "href="PART I WANT TO EXTRACT">[link]" | awk -F""" '{print $2}'
PART I WANT TO EXTRACT
Ou shell en utilisant lui-même
$ a="href="PART I WANT TO EXTRACT">[link]"
$ a=${a//"/}
$ echo ${a/&*/}
PART I WANT TO EXTRACT
Voici une autre façon Bash:
$ string="href="PART I WANT TO EXTRACT">[link]"
$ entity="""
$ string=${string#*${entity}*}
$ string=${string%*${entity}*}
$ echo $string
PART I WANT TO EXTRACT
Ceci illustre deux caractéristiques: Supprimer le préfixe correspondant/modèle de suffixe et la utilisation d'une variable pour contenir le motif (vous pouvez utiliser un littéral à la place).
De quel shell parlez-vous? –
Oh désolé, bash. – Josh