184 votes

Comment utiliser sed/grep pour extraire le texte entre deux mots ?

J'essaie de produire une chaîne qui contient tout ce qui se trouve entre deux mots d'une chaîne :

l'entrée :

"Here is a String"

sortie :

"is a"

Utilisation :

sed -n '/Here/,/String/p'

inclut les points de terminaison, mais je ne veux pas les inclure.

4voto

mvairavan Points 129

Vous pouvez utiliser \1 (voir http://www.grymoire.com/Unix/Sed.html#uh-4 ):

echo "Hello is a String" | sed 's/Hello\(.*\)String/\1/g'

Le contenu qui se trouve à l'intérieur des crochets sera stocké en tant que \1 .

1voto

Victoria Stuart Points 21

Problème. Les messages Claws Mail que j'ai stockés sont présentés comme suit, et j'essaie d'extraire les lignes Objet :

Subject: [SLC38A9 lysosomal arginine sensor; mTORC1 pathway] Key molecular
 link in major cell growth pathway: Findings point to new potential
 therapeutic target in pancreatic cancer [mTORC1 Activator SLC38A9 Is
 Required to Efflux Essential Amino Acids from Lysosomes and Use Protein as
 a Nutrient] [Re: Nutrient sensor in key growth-regulating metabolic pathway
 identified [Lysosomal amino acid transporter SLC38A9 signals arginine
 sufficiency to mTORC1]]
Message-ID: <20171019190902.18741771@VictoriasJourney.com>

Par A2 dans ce fil, Comment utiliser sed/grep pour extraire le texte entre deux mots ? la première expression, ci-dessous, "fonctionne" tant que le texte correspondant ne contient pas de nouvelle ligne :

grep -o -P '(?<=Subject: ).*(?=molecular)' corpus/01

[SLC38A9 lysosomal arginine sensor; mTORC1 pathway] Key

Cependant, malgré l'essai de nombreuses variantes ( .+?; /s; ... ), je n'ai pas réussi à les faire fonctionner :

grep -o -P '(?<=Subject: ).*(?=link)' corpus/01
grep -o -P '(?<=Subject: ).*(?=therapeutic)' corpus/01
etc.

Solution 1.

Par Extraire le texte entre deux chaînes de caractères sur des lignes différentes

sed -n '/Subject: /{:a;N;/Message-ID:/!ba; s/\n/ /g; s/\s\s*/ /g; s/.*Subject: \|Message-ID:.*//g;p}' corpus/01

ce qui donne

[SLC38A9 lysosomal arginine sensor; mTORC1 pathway] Key molecular link in major cell growth pathway: Findings point to new potential therapeutic target in pancreatic cancer [mTORC1 Activator SLC38A9 Is Required to Efflux Essential Amino Acids from Lysosomes and Use Protein as a Nutrient] [Re: Nutrient sensor in key growth-regulating metabolic pathway identified [Lysosomal amino acid transporter SLC38A9 signals arginine sufficiency to mTORC1]]                              

Solution 2.*

Par Comment puis-je remplacer un saut de ligne ( \n ) en utilisant sed ?

sed ':a;N;$!ba;s/\n/ /g' corpus/01

remplacera les nouvelles lignes par un espace.

Enchaîner avec A2 dans Comment utiliser sed/grep pour extraire le texte entre deux mots ? on obtient :

sed ':a;N;$!ba;s/\n/ /g' corpus/01 | grep -o -P '(?<=Subject: ).*(?=Message-ID:)'

ce qui donne

[SLC38A9 lysosomal arginine sensor; mTORC1 pathway] Key molecular  link in major cell growth pathway: Findings point to new potential  therapeutic target in pancreatic cancer [mTORC1 Activator SLC38A9 Is  Required to Efflux Essential Amino Acids from Lysosomes and Use Protein as  a Nutrient] [Re: Nutrient sensor in key growth-regulating metabolic pathway  identified [Lysosomal amino acid transporter SLC38A9 signals arginine  sufficiency to mTORC1]] 

Cette variante supprime les espaces doubles :

sed ':a;N;$!ba;s/\n/ /g; s/\s\s*/ /g' corpus/01 | grep -o -P '(?<=Subject: ).*(?=Message-ID:)'

donner

[SLC38A9 lysosomal arginine sensor; mTORC1 pathway] Key molecular link in major cell growth pathway: Findings point to new potential therapeutic target in pancreatic cancer [mTORC1 Activator SLC38A9 Is Required to Efflux Essential Amino Acids from Lysosomes and Use Protein as a Nutrient] [Re: Nutrient sensor in key growth-regulating metabolic pathway identified [Lysosomal amino acid transporter SLC38A9 signals arginine sufficiency to mTORC1]]

0voto

kenorb Points 2464

ripgrep

Voici l'exemple utilisant rg :

$ echo Here is a String | rg 'Here\s(.*)\sString' -r '$1'
is a

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X