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 > Pattern matching question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Pattern matching question

Réponse
 
LinkBack Outils de la discussion
Vieux 08/01/2008, 01h19   #1
Architect
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Pattern matching question

Hello everyone and happy new year,

I have a file in the following format

Thu Jul 18 03:44:03 2007
xxx
yyy
zzz
Sun Jun 1 01:00:13 2007
a
b
Mon Jun 2 02:04:00 2007
x = xxxx
z = zzzz
zx = xz

Data between date's are random ,can be anything .I want it in this
format .

Thu Jul 18 03:44:03 2007 xxx yyy zzz
Sun Jun 1 01:00:13 2007 a b
Mon Jun 2 02:04:00 2007 x = xxxx z = zzzz zx = xz

Simply,for every date ,join every line till next date . My problem is
how to join till next date . Let me saw u what i did .

1) i turned the result into 1 line using tr '\n' ' ' so now i have all
the file joined together

Thu Jul 18 03:44:03 2007 xxx yyy zzz Sun Jun 1 01:00:13 2007 a b Mon
Jun 2 02:04:00 2007 x = xxxx ....
^
^ ^
and now i thought to put a new line before every date's 1st letter.I
did this in sed

doit.sed:
/^[A-Z][a-z]\{3\} [A-Z][a-z]\{3\} [0-9]\{1,2\} [0-9]\{1,2\}:
[0-9]\{1,2\}:[0-9]\{1,2\} [0-9]\{4\}/i\
\n

then i do ,cat INPUT_FILE |tr '\n' ' ' | sed -f doit.sed but i get the
same line ... i don't know if i am missing something here. Is there a
better way to accomplish this ?


Thanks in advance
  Réponse avec citation
Vieux 08/01/2008, 04h03   #2
Barry Margolin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pattern matching question

In article
<a4ec4cd7-9bed-4a9f-92f9-77ddee436644@d21g2000prf.googlegroups.com>,
Architect <x31337@gmail.com> wrote:

> Hello everyone and happy new year,
>
> I have a file in the following format
>
> Thu Jul 18 03:44:03 2007
> xxx
> yyy
> zzz
> Sun Jun 1 01:00:13 2007
> a
> b
> Mon Jun 2 02:04:00 2007
> x = xxxx
> z = zzzz
> zx = xz
>
> Data between date's are random ,can be anything .I want it in this
> format .
>
> Thu Jul 18 03:44:03 2007 xxx yyy zzz
> Sun Jun 1 01:00:13 2007 a b
> Mon Jun 2 02:04:00 2007 x = xxxx z = zzzz zx = xz
>
> Simply,for every date ,join every line till next date . My problem is
> how to join till next date . Let me saw u what i did .
>
> 1) i turned the result into 1 line using tr '\n' ' ' so now i have all
> the file joined together
>
> Thu Jul 18 03:44:03 2007 xxx yyy zzz Sun Jun 1 01:00:13 2007 a b Mon
> Jun 2 02:04:00 2007 x = xxxx ....
> ^
> ^ ^
> and now i thought to put a new line before every date's 1st letter.I
> did this in sed
>
> doit.sed:
> /^[A-Z][a-z]\{3\} [A-Z][a-z]\{3\} [0-9]\{1,2\} [0-9]\{1,2\}:
> [0-9]\{1,2\}:[0-9]\{1,2\} [0-9]\{4\}/i\
> \n
>
> then i do ,cat INPUT_FILE |tr '\n' ' ' | sed -f doit.sed but i get the
> same line ... i don't know if i am missing something here. Is there a
> better way to accomplish this ?
>
>
> Thanks in advance


Day and month names are one uppercase letter followed by two lowercase
letters. Your regexp looks for THREE lowercase letters, so it doesn't
match them.

Anyway, it would probably be easier to do this using awk:

awk 'NR > 1 && /^([A-Z][a-z]{2} ){2} ([0-9]{2}{2}[0-9]{2} [0-9]{4}/
{printf("\n%s ", $0)} \
{printf("%s " $0)} \
END {printf("\n")}'

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
  Réponse avec citation
Vieux 08/01/2008, 05h07   #3
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pattern matching question

Barry Margolin wrote:

> In article
> <a4ec4cd7-9bed-4a9f-92f9-77ddee436644@d21g2000prf.googlegroups.com>,
> Architect <x31337@gmail.com> wrote:
>
>
>>Hello everyone and happy new year,
>>
>>I have a file in the following format
>>
>>Thu Jul 18 03:44:03 2007
>>xxx
>>yyy
>>zzz
>>Sun Jun 1 01:00:13 2007
>>a
>>b
>>Mon Jun 2 02:04:00 2007
>>x = xxxx
>>z = zzzz
>>zx = xz
>>
>>Data between date's are random ,can be anything .I want it in this
>>format .
>>
>>Thu Jul 18 03:44:03 2007 xxx yyy zzz
>>Sun Jun 1 01:00:13 2007 a b
>>Mon Jun 2 02:04:00 2007 x = xxxx z = zzzz zx = xz
>>
>>Simply,for every date ,join every line till next date . My problem is
>>how to join till next date . Let me saw u what i did .
>>
>>1) i turned the result into 1 line using tr '\n' ' ' so now i have all
>>the file joined together
>>
>>Thu Jul 18 03:44:03 2007 xxx yyy zzz Sun Jun 1 01:00:13 2007 a b Mon
>>Jun 2 02:04:00 2007 x = xxxx ....
>>^
>>^ ^
>>and now i thought to put a new line before every date's 1st letter.I
>>did this in sed
>>
>>doit.sed:
>>/^[A-Z][a-z]\{3\} [A-Z][a-z]\{3\} [0-9]\{1,2\} [0-9]\{1,2\}:
>>[0-9]\{1,2\}:[0-9]\{1,2\} [0-9]\{4\}/i\
>>\n
>>
>>then i do ,cat INPUT_FILE |tr '\n' ' ' | sed -f doit.sed but i get the
>>same line ... i don't know if i am missing something here. Is there a
>>better way to accomplish this ?
>>
>>
>>Thanks in advance

>
>
> Day and month names are one uppercase letter followed by two lowercase
> letters. Your regexp looks for THREE lowercase letters, so it doesn't
> match them.
>
> Anyway, it would probably be easier to do this using awk:
>
> awk 'NR > 1 && /^([A-Z][a-z]{2} ){2} ([0-9]{2}{2}[0-9]{2} [0-9]{4}/
> {printf("\n%s ", $0)} \
> {printf("%s " $0)} \
> END {printf("\n")}'
>


For the OP,just be aware that you need an awk that supports RE intervals
for that to work (e.g. a POSIX awk). If you're using GNU awk you'd add
"--re-interval" as an option to enable that functionality. I tweaked it
slightly too to fix one bug, remove a little redundant code and avoid
adding trailing white space:

gawk --re-interval '
NR > 1 && /^([A-Z][a-z]{2} ){2} ([0-9]{2}{2}[0-9]{2} [0-9]{4}/ {s=ORS}
{printf "%s%s",s,$0; s=OFS}
END {print ""}'

Regards,

Ed.
  Réponse avec citation
Vieux 08/01/2008, 06h42   #4
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pattern matching question

On Jan 8, 9:19 am, Architect <x31...@gmail.com> wrote:
> Hello everyone and happy new year,
>
> I have a file in the following format
>
> Thu Jul 18 03:44:03 2007
> xxx
> yyy
> zzz
> Sun Jun 1 01:00:13 2007
> a
> b
> Mon Jun 2 02:04:00 2007
> x = xxxx
> z = zzzz
> zx = xz
>
> Data between date's are random ,can be anything .I want it in this
> format .
>
> Thu Jul 18 03:44:03 2007 xxx yyy zzz
> Sun Jun 1 01:00:13 2007 a b
> Mon Jun 2 02:04:00 2007 x = xxxx z = zzzz zx = xz
>
> Simply,for every date ,join every line till next date . My problem is
> how to join till next date . Let me saw u what i did .
>
> 1) i turned the result into 1 line using tr '\n' ' ' so now i have all
> the file joined together
>
> Thu Jul 18 03:44:03 2007 xxx yyy zzz Sun Jun 1 01:00:13 2007 a b Mon
> Jun 2 02:04:00 2007 x = xxxx ....
> ^
> ^ ^
> and now i thought to put a new line before every date's 1st letter.I
> did this in sed
>
> doit.sed:
> /^[A-Z][a-z]\{3\} [A-Z][a-z]\{3\} [0-9]\{1,2\} [0-9]\{1,2\}:
> [0-9]\{1,2\}:[0-9]\{1,2\} [0-9]\{4\}/i\
> \n
>
> then i do ,cat INPUT_FILE |tr '\n' ' ' | sed -f doit.sed but i get the
> same line ... i don't know if i am missing something here. Is there a
> better way to accomplish this ?
>
> Thanks in advance


while read line
do
case $line in
Mon*|Tue*|Wed*|Thu*|Fri*|Sat*|Sun* ) printf "\n$line ";;
*) printf "$line ";;
esac
done < "file"
  Réponse avec citation
Vieux 08/01/2008, 07h45   #5
Rakesh Sharma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pattern matching question

On Jan 8, 6:19 am, Architect <x31...@gmail.com> wrote:
> Hello everyone and happy new year,
>
> I have a file in the following format
>
> Thu Jul 18 03:44:03 2007
> xxx
> yyy
> zzz
> Sun Jun 1 01:00:13 2007
> a
> b
> Mon Jun 2 02:04:00 2007
> x = xxxx
> z = zzzz
> zx = xz
>
> Data between date's are random ,can be anything .I want it in this
> format .
>
> Thu Jul 18 03:44:03 2007 xxx yyy zzz
> Sun Jun 1 01:00:13 2007 a b
> Mon Jun 2 02:04:00 2007 x = xxxx z = zzzz zx = xz
>
> Simply,for every date ,join every line till next date . My problem is
> how to join till next date . Let me saw u what i did .
>
> 1) i turned the result into 1 line using tr '\n' ' ' so now i have all
> the file joined together
>
> Thu Jul 18 03:44:03 2007 xxx yyy zzz Sun Jun 1 01:00:13 2007 a b Mon
> Jun 2 02:04:00 2007 x = xxxx ....
> ^
> ^ ^
> and now i thought to put a new line before every date's 1st letter.I


sed -ne '
/^[A-Z]/{
h
:loop
n
$bend
/^[A-Z]/bend
H
bloop
}
:end
x
s/\n/ /g
p
bloop
' < yourfile
  Réponse avec citation
Vieux 08/01/2008, 14h01   #6
Architect
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pattern matching question

thanks a lot guys ,i figured my mistakes .
  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 14h18.


É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,16698 seconds with 14 queries