Pattern matching question

Pattern matching question

am 08.01.2008 02:19:26 von x31337

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

Re: Pattern matching question

am 08.01.2008 05:03:40 von Barry Margolin

In article
,
Architect 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 ***

Re: Pattern matching question

am 08.01.2008 06:07:02 von Ed Morton

Barry Margolin wrote:

> In article
> ,
> Architect 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.

Re: Pattern matching question

am 08.01.2008 07:42:54 von mik3l3374

On Jan 8, 9:19 am, Architect 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"

Re: Pattern matching question

am 08.01.2008 08:45:25 von Rakesh Sharma

On Jan 8, 6:19 am, Architect 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

Re: Pattern matching question

am 08.01.2008 15:01:11 von x31337

thanks a lot guys ,i figured my mistakes .