PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > selecting text between two patterns
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

selecting text between two patterns

Réponse
 
LinkBack Outils de la discussion
Vieux 02/09/2007, 16h54   #1
gin_g
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut selecting text between two patterns

Hey... I'm trying to extract text between two patterns. Ideally,
if I had two patterns:
PATTERN1
PATTERN2
I'd like to get back the text that is between the first occurance of
those patterns, not including the patterns
For instance, if I had the text:

this is one line
this is PATTERN1 another line
hello
this is somethingPATTERN2 else
this is the last line

I would want this result

another line
hello
this is something

Or if I had this text:
this PATTERN1is one big line. But it isPATTERN2 not very long.

I would want this result:
is one big line. But it is


Thanks!

  Réponse avec citation
Vieux 02/09/2007, 18h47   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns

On 2007-09-02, gin_g wrote:
> Hey... I'm trying to extract text between two patterns. Ideally,
> if I had two patterns:
> PATTERN1
> PATTERN2
> I'd like to get back the text that is between the first occurance of
> those patterns, not including the patterns
> For instance, if I had the text:
>
> this is one line
> this is PATTERN1 another line
> hello
> this is somethingPATTERN2 else
> this is the last line
>
> I would want this result
>
> another line
> hello
> this is something
>
> Or if I had this text:
> this PATTERN1is one big line. But it isPATTERN2 not very long.
>
> I would want this result:
> is one big line. But it is


This may not work with very large files:

file=$( cat "$FILENAME" )
temp=${file#*PATTERN1}
printf "%s\n" "${temp%%PATTERN2*}"


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 02/09/2007, 23h46   #3
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns

gin_g wrote:
> Hey... I'm trying to extract text between two patterns. Ideally,
> if I had two patterns:
> PATTERN1
> PATTERN2
> I'd like to get back the text that is between the first occurance of
> those patterns, not including the patterns
> For instance, if I had the text:
>
> this is one line
> this is PATTERN1 another line
> hello
> this is somethingPATTERN2 else
> this is the last line
>
> I would want this result
>
> another line
> hello
> this is something


$ echo "this is one line
this is PATTERN1 another line
hello
this is somethingPATTERN2 else
this is the last line
" | perl -lne'print if s/.*?PATTERN1// .. s/(.*)PATTERN2.*/$1/'
another line
hello
this is something


> Or if I had this text:
> this PATTERN1is one big line. But it isPATTERN2 not very long.
>
> I would want this result:
> is one big line. But it is


$ echo "this PATTERN1is one big line. But it isPATTERN2 not very long.
" | perl -lne'print if s/.*?PATTERN1// .. s/(.*)PATTERN2.*/$1/'
is one big line. But it is




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
  Réponse avec citation
Vieux 02/09/2007, 23h46   #4
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns

gin_g wrote:
> Hey... I'm trying to extract text between two patterns. Ideally,
> if I had two patterns:
> PATTERN1
> PATTERN2
> I'd like to get back the text that is between the first occurance of
> those patterns, not including the patterns
> For instance, if I had the text:
>
> this is one line
> this is PATTERN1 another line
> hello
> this is somethingPATTERN2 else
> this is the last line
>
> I would want this result
>
> another line
> hello
> this is something


$ echo "this is one line
this is PATTERN1 another line
hello
this is somethingPATTERN2 else
this is the last line
" | perl -lne'print if s/.*?PATTERN1// .. s/(.*)PATTERN2.*/$1/'
another line
hello
this is something


> Or if I had this text:
> this PATTERN1is one big line. But it isPATTERN2 not very long.
>
> I would want this result:
> is one big line. But it is


$ echo "this PATTERN1is one big line. But it isPATTERN2 not very long.
" | perl -lne'print if s/.*?PATTERN1// .. s/(.*)PATTERN2.*/$1/'
is one big line. But it is




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
  Réponse avec citation
Vieux 03/09/2007, 00h46   #5
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns

On Sep 2, 10:54 am, gin_g <ginger.m.grif...@gmail.com> wrote:
> Hey... I'm trying to extract text between two patterns. Ideally,
> if I had two patterns:
> PATTERN1
> PATTERN2
> I'd like to get back the text that is between the first occurance of
> those patterns, not including the patterns
> For instance, if I had the text:
>
> this is one line
> this is PATTERN1 another line
> hello
> this is somethingPATTERN2 else
> this is the last line
>
> I would want this result
>
> another line
> hello
> this is something
>
> Or if I had this text:
> this PATTERN1is one big line. But it isPATTERN2 not very long.
>
> I would want this result:
> is one big line. But it is
>
> Thanks!


ruby -e 'puts gets(nil)[ /PATTERN1(.*)PATTERN2/m,1]' my_file

The first occurrence of PATTERN1 is used, and
the last of PATTERN2.

==== input ====
one
twoPATTERN1
three PATTERN1
four
fivePATTERN2
sixPATTERN2
seven

==== output ====

three PATTERN1
four
fivePATTERN2
six

  Réponse avec citation
Vieux 03/09/2007, 02h28   #6
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns

gin_g wrote:
> Hey... I'm trying to extract text between two patterns. Ideally,
> if I had two patterns:
> PATTERN1
> PATTERN2
> I'd like to get back the text that is between the first occurance of
> those patterns, not including the patterns
> For instance, if I had the text:
>
> this is one line
> this is PATTERN1 another line
> hello
> this is somethingPATTERN2 else
> this is the last line
>
> I would want this result
>
> another line
> hello
> this is something
>
> Or if I had this text:
> this PATTERN1is one big line. But it isPATTERN2 not very long.
>
> I would want this result:
> is one big line. But it is
>
>
> Thanks!
>


With an awk that supports REs as Record Separators (e.g. gawk), it might
be just:

awk -v RS="PATTERN1|PATTERN2" 'NR==2' file

depending on whether or not PATTERN1 or PATTERN2 can occur before
PATTERN1 in your real input.

Regards,

Ed.
  Réponse avec citation
Vieux 06/09/2007, 06h46   #7
Heinz Müller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns


"Ed Morton" <morton@lsupcaemnt.com> schrieb im Newsbeitrag
news:eoCdnXjGT6jc_kbbnZ2dnUVZ_oGjnZ2d@comcast.com. ..
>
> awk -v RS="PATTERN1|PATTERN2" 'NR==2' file
>
> depending on whether or not PATTERN1 or PATTERN2 can occur before PATTERN1
> in your real input.
>
> Regards,
>
> Ed.


And what is the meaning of 'NR==2' in that awk expression?

Regards,
Heinz


  Réponse avec citation
Vieux 06/09/2007, 09h46   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns

Heinz Müller wrote:
> "Ed Morton" <morton@lsupcaemnt.com> schrieb im Newsbeitrag
> news:eoCdnXjGT6jc_kbbnZ2dnUVZ_oGjnZ2d@comcast.com. ..
>
>>awk -v RS="PATTERN1|PATTERN2" 'NR==2' file
>>
>>depending on whether or not PATTERN1 or PATTERN2 can occur before PATTERN1
>>in your real input.
>>
>>Regards,
>>
>>Ed.

>
>
> And what is the meaning of 'NR==2' in that awk expression?


It's a test for the second record in the file, so if you have a file like:

abc
PATTERN1
def
ghi
PATTERN2
klm

then since I'm specifying that the Record Separator (RS) is the RE
"PATTERN1 or PATTERN2" the first record will be

abc

and the second will be:

def
ghi

and the third will be:

klm

So, by testing for the Number of Records (NR) equal to 2, I'm selecting
that second record. Since I don't specify any action to take when that
condition (NR==2) is true, awk uses the default action which is to just
print the record for which that condition is true.

Ed.
  Réponse avec citation
Vieux 06/09/2007, 20h56   #9
Heinz Müller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns


"Ed Morton" <morton@lsupcaemnt.com> schrieb im Newsbeitrag
news:_YudnXwzhO_nI0LbnZ2dnUVZ_oSnnZ2d@comcast.com. ..
>
> It's a test for the second record in the file, so if you have a file like:
>
> abc
> PATTERN1
> def
> ghi
> PATTERN2
> klm
>
> then since I'm specifying that the Record Separator (RS) is the RE
> "PATTERN1 or PATTERN2" the first record will be
>
> abc
>
> and the second will be:
>
> def
> ghi
>
> and the third will be:
>
> klm
>
> So, by testing for the Number of Records (NR) equal to 2, I'm selecting
> that second record. Since I don't specify any action to take when that
> condition (NR==2) is true, awk uses the default action which is to just
> print the record for which that condition is true.
>
> Ed.


Thank you for the detailed eplanation!!



  Réponse avec citation
Vieux 07/09/2007, 21h10   #10
William Park
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: selecting text between two patterns

gin_g <ginger.m.griffin@gmail.com> wrote:
> Hey... I'm trying to extract text between two patterns. Ideally,
> if I had two patterns:
> PATTERN1
> PATTERN2
> I'd like to get back the text that is between the first occurance of
> those patterns, not including the patterns
> For instance, if I had the text:
>
> this is one line
> this is PATTERN1 another line
> hello
> this is somethingPATTERN2 else
> this is the last line
>
> I would want this result
>
> another line
> hello
> this is something
>
> Or if I had this text:
> this PATTERN1is one big line. But it isPATTERN2 not very long.
>
> I would want this result:
> is one big line. But it is


If you're brave, you can try my Bash extension:
http://freshmeat.net/projects/bashdiff/
http://home.eol.ca/~parkw/index.html#strinterval

strinterval [-r] string begin end [submatch]
Extract substring, delimited by non-overlapping BEGIN and END patterns. By
default, the patterns are simple string, but regex(7) pattern can be used
with -r option. Returns success (0) if patterns are found, or failure (1)
if patterns are not found. When patterns are found, there are 5 segments
to consider:
string --> submatch=( prefix BEGIN middle END suffix )
All 5 segments are returned in array variable SUBMATCH (if given) which is
always flushed first.

Eg.
a='this PATTERN1is one big line. But it isPATTERN2 not very long'
strinterval "$a" "PATTERN1" "PATTERN2" submatch
declare -p submatch

==> submatch='([0]="this " [1]="PATTERN1" [2]="is one big line. But it is"
[3]="PATTERN2" [4]=" not very long")'

So, you want
echo "${submatch[2]}"

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20474 seconds with 18 queries