Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing a paragraph by a string

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
Hi,

I have the following long text:
================
!:: Start ::
!Job Ticket Editor
!25-September-2002 16:56:00
!ripro_messages
!DB file does not exist.
!cannot open Job Ticket Editor.:: eom ::
!E
!Ok
!:: End ::
!:: Start ::
!Undefined
!25-September-2002 16:56:18
!ripro_messages
!Station timna1 has different software version.:: eom ::
!W
!Ok
!:: End ::
!:: Start ::
!Store
!25-September-2002 16:56:45
!/dataVolumes/timna10.4.3/from_HF6/cards4.job#25165555.jt
!Store - error reading assign file
!/dataVolumes/timna10.4.3/from_HF6/cards4.job
! Please check all the necessary mounts.:: eom ::
!E
!Ok
!:: End ::
!:: Start ::
!Undefined
!25-September-2002 16:58:01
!ripro_messages
!Station timna1 has different software version.:: eom ::
!W
!Ok
!:: End ::
================================
I want to print out all paragrahps starting with "!:: Start ::" and ending with "!:: End ::",that include inside the pattern "error".

How would I do it ? "Long live king Moshiach !"
h
 
One way .....UNTESTED
awk '
BEGIN {
n = 0
fn = new_error_file
}
{
if ($0 ~ /error/)
a[n++] = $0
if ($0 ~ "!:: End ::")
{
for(i=0;i<n;i++)
print a >> fn
n = 0
}
}' error_file

HTH ;-) Dickie Bird
Honi soit qui mal y pense
 
DAMMIT - forgot rules
One way .....UNTESTED
awk '
BEGIN {
n = 0
fn = new_error_file
}
{
if ($0 ~ /error/)
a[n++] = $0
if ($0 ~ &quot;!:: End ::&quot;)
{
for(i=0;i<n;i++)
print a >> fn
n = 0
}
}' error_file

HTH ;-) Dickie Bird
Honi soit qui mal y pense
 
Here's my attempt.

{
if ($0 ~ /^!:: Start ::/) ix = 1
if ($0 ~ /error/) {
for (j=1;j<=ix;j++) print a[j]
k = 1
ix = 0
}
if (ix) a[ix++] = $0
if (k) print
if ($0 ~ /^!:: End ::/) {
ix = 0
k = 0
}
} CaKiwi
 
sed -ne '/:: Start ::/,/:: Start ::/p' -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
sorry, the 2. Start is End -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Thanks to all for reply.

Actualy,I wa slooking for a one line command.
Jamisar,the command I'm looking for should include the search ONLY for paragraphs that include the string &quot;error&quot; - how do I achieve this ?

&quot;Long live king Moshiach !&quot;
h
 
i dont believe you can do this in a one-liner
sed reads line by line applies cmds on them but dont remember them nor can set flags...
awk can this, you only need the right code
dickiebird's awk is a good approach, needs more work
if you are familiar with 'c' a 30-40 line code will do it.
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Here's an alternative using csplit - probably less efficient than awk though.

#!/usr/bin/ksh
occurrences=`grep Start your_input_file | wc -l | sed 's/ //g'`
i=0

/usr/ucb/echo -n &quot;csplit your_input_file &quot; > output

while [ $i -ne $occurrences ]
do
/usr/ucb/echo -n &quot;/Start/ &quot; >> output
((i += 1))
done

sh output >/dev/null

for file in `grep -l error xx*`
do
cat $file
done

#tidy up files created by csplit
rm xx*
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top