|
|||
|
I am cutting required pages detemined with conditions . Is there a better solution under awk for sucj task ?
My program like this : BEGIN { fn=ENVIRON["filename"] codeawk=ENVIRON["code"] gsub(/\\/,"/",fn) q=fn codeawk"_data_P.txt" old=ENVIRON["filename"] codeawk"_data_P.log" system ("del "old"") q1=ENVIRON["filename2"] flag="N" countera=1 old_checked_account="" "echo %date%" | getline print_date "echo %time%" | getline print_time legend=substr($0,38,18) if (codeawk==101) req_branch="القاهرة" if (codeawk==102) req_branch="الموسكي" if (codeawk==103) req_branch="شريف" if (codeawk==104) req_branch="التحرير" if (codeawk==105) req_branch="مصر الجديدة" if (codeawk==106) req_branch="الزمالك" if (codeawk==107) req_branch="حلوان" if (codeawk==108) req_branch="غمرة" if (codeawk==109) req_branch="النزهة" if (codeawk==111) req_branch="المعادي" if (codeawk==112) req_branch="العباسية" if (codeawk==113) req_branch="الجيزة" if (codeawk==114) req_branch="الفلكي" if (codeawk==115) req_branch="الدقي" if (codeawk==117) req_branch="شبرا" if (codeawk==118) req_branch="شبرا الخيمة" if (codeawk==120) req_branch="روكسي" if (codeawk==120) req_branch="روكسي" if (codeawk==121) req_branch="مدينة نصر" if (codeawk==122) req_branch="مدينة نصر" if (codeawk==123) req_branch="حدائق القبة" if (codeawk==123) req_branch="حدائق القبة" if (codeawk==124) req_branch="الخانكة" if (codeawk==125) req_branch="26يوليو" if (codeawk==126) req_branch="الهرم" if (codeawk==127) req_branch="الحوامدية" if (codeawk==128) req_branch="15مايو" if (codeawk==129) req_branch="6أكتوبر" if (codeawk==130) req_branch="العاشر من رمضان" if (codeawk==144) req_branch="هيئة الاستثمار" if (codeawk==146) req_branch="الفاو" if (codeawk==147) req_branch="6 اكتوبر الحى المتميز" if (codeawk==148) req_branch="فرع ش جامعة الدول العربية" if (codeawk==149) req_branch="فرع الحرية" if (codeawk==150) req_branch="اركاديا مول" if (codeawk==151) req_branch="مساكن الشيراتون" if (codeawk==152) req_branch="مصر والسودان" if (codeawk==153) req_branch="النزهة الجديدة" if (codeawk==154) req_branch="النيل بالعجوزة" if (codeawk==155) req_branch="فرع البحر الأعظم" if (codeawk==156) req_branch="فرع سيتى ستارز" if (codeawk==157) req_branch="فرع سيتى كلوب مول" if (codeawk==158) req_branch="الخليفة المأمون" if (codeawk==159) req_branch="فرع المنيل" if (codeawk==160) req_branch="الحى الأول بالعاشر من رمضان" if (codeawk==162) req_branch="جاردن سيتى" if (codeawk==163) req_branch="الزمالك الجديد" if (codeawk==201) req_branch="الاسكندرية" if (codeawk==202) req_branch="سعد زغلول" if (codeawk==203) req_branch="سيدي بشر" if (codeawk==204) req_branch="مينا البصل" if (codeawk==205) req_branch="طلعت حرب" if (codeawk==206) req_branch="سبورتنج" if (codeawk==207) req_branch="السادات" if (codeawk==208) req_branch="مرسى مطروح" if (codeawk==209) req_branch="برج العرب" if (codeawk==210) req_branch="ابو قير" if (codeawk==211) req_branch="الدخيلة" if (codeawk==212) req_branch="جليم" if (codeawk==213) req_branch="دمنهور" if (codeawk==214) req_branch="رشيد" if (codeawk==215) req_branch="كفر الدوار" if (codeawk==216) req_branch="ابو حمص" if (codeawk==217) req_branch="وادي النطرون" if (codeawk==218) req_branch="سماد ابو قير" if (codeawk==218) req_branch="سماد ابوقير" if (codeawk==219) req_branch="النوبارية" if (codeawk==220) req_branch="توكيل نادي القضاة" if (codeawk==221) req_branch="فرع سيزوستريس" if (codeawk==223) req_branch="كيرو سيزو مول" if (codeawk==301) req_branch="المنصورة" if (codeawk==302) req_branch="المحلة" if (codeawk==303) req_branch="طنطا" if (codeawk==304) req_branch="الزقازيق" if (codeawk==305) req_branch="بنها" if (codeawk==306) req_branch="المأمون" if (codeawk==307) req_branch="دمياط" if (codeawk==308) req_branch="ميت غمر" if (codeawk==309) req_branch="شبين الكوم" if (codeawk==310) req_branch="دسوق" if (codeawk==311) req_branch="كفر الشيخ" if (codeawk==312) req_branch="كفر الزيات" if (codeawk==313) req_branch="دكرنس" if (codeawk==314) req_branch="فارسكور" if (codeawk==315) req_branch="السلام" if (codeawk==315) req_branch="السلام" if (codeawk==316) req_branch="دمياط الجديدة" if (codeawk==317) req_branch="سمنود" if (codeawk==318) req_branch="فاقوس" if (codeawk==319) req_branch="بلقاس" if (codeawk==319) req_branch="بلقاس" if (codeawk==320) req_branch="ههيا" if (codeawk==321) req_branch="شربين" if (codeawk==322) req_branch="توكيل قها" if (codeawk==323) req_branch="نبروه" if (codeawk==324) req_branch="الباجور" if (codeawk==325) req_branch="قويسنا" if (codeawk==326) req_branch="السنبلاوين" if (codeawk==327) req_branch="الحامول" if (codeawk==328) req_branch="طوخ" if (codeawk==329) req_branch="منطقة مبارك الصناعية بقويسنا" if (codeawk==331) req_branch="ميناء دمياط" if (codeawk==332) req_branch="ش الجميل" if (codeawk==333) req_branch="الجلاء بدمياط" if (codeawk==335) req_branch="البحر بكفر الزيات" if (codeawk==401) req_branch="الفيوم" if (codeawk==401) req_branch="الفيوم" if (codeawk==402) req_branch="بني سويف" if (codeawk==403) req_branch="المنيا" if (codeawk==404) req_branch="اسيوط" if (codeawk==405) req_branch="سوهاج" if (codeawk==406) req_branch="جرجا" if (codeawk==407) req_branch="قنا" if (codeawk==408) req_branch="الاقصر" if (codeawk==409) req_branch="اسوان" if (codeawk==410) req_branch="ببا" if (codeawk==411) req_branch="ديروط" if (codeawk==412) req_branch="قوص" if (codeawk==413) req_branch="المنشأة" if (codeawk==414) req_branch="نجع حمادي" if (codeawk==415) req_branch="طهطا" if (codeawk==415) req_branch="طهطا" if (codeawk==416) req_branch="القوصية" if (codeawk==417) req_branch="طما" if (codeawk==418) req_branch="ملوى" if (codeawk==419) req_branch="كوم امبو" if (codeawk==420) req_branch="منفلوط" if (codeawk==421) req_branch="ابو تيج" if (codeawk==422) req_branch="ابنوب" if (codeawk==423) req_branch="البلينا" if (codeawk==424) req_branch="مغاغة" if (codeawk==425) req_branch="سمالوط" if (codeawk==425) req_branch="سمالوط" if (codeawk==426) req_branch="ابوقرقاص" if (codeawk==427) req_branch="بني مزار" if (codeawk==428) req_branch="دشنا" if (codeawk==429) req_branch="اسنا" if (codeawk==430) req_branch="توكيل سنورس" if (codeawk==431) req_branch="فــرع البدارى" if (codeawk==433) req_branch="توكيل ابوتشت" if (codeawk==434) req_branch="توكيل مجمع الالومنيوم" if (codeawk==435) req_branch="ساقلتة" if (codeawk==436) req_branch="توكيل دار السلام" if (codeawk==437) req_branch="ادفو" if (codeawk==438) req_branch="جمهورية الفيوم" if (codeawk==439) req_branch="توكيل خالد بن الوليد" if (codeawk==440) req_branch="بنى سويف الجديدة" if (codeawk==444) req_branch="فرع 23 يوليو بقننا" if (codeawk==501) req_branch="بورسعيد" if (codeawk==502) req_branch="الاسماعيلية" if (codeawk==503) req_branch="السويس" if (codeawk==504) req_branch="العريش" if (codeawk==505) req_branch="القنطرة غرب" if (codeawk==506) req_branch="المنطقة الصناعية" if (codeawk==507) req_branch="بور توفيق" if (codeawk==508) req_branch="رفح" if (codeawk==510) req_branch="شرم الشيخ" if (codeawk==511) req_branch="سفاجا" if (codeawk==512) req_branch="نويبع" if (codeawk==513) req_branch="طور سيناء" if (codeawk==514) req_branch="توكيل بور فؤاد" if (codeawk==515) req_branch="توكيل الشيخ زايد" if (codeawk==516) req_branch="توكيل الصباح" if (codeawk==518) req_branch="المجمع التجارى خليج نعمة" if (codeawk==519) req_branch="فرع سنتر المكرمية" } { #print "req_branch",req_branch legend=substr($0,38,18) if (legend =="BANK OF ALEXANDRIA") { #----------------------------------- # #definitions for 2 OMR #---page---------------------- #end_cut=NR+48 #afterP2f_2o=NR-31 #afterP2t_2o=afterP2f_2o+16 #---page---------------------- # cutP2f_1o= NR-34 # cutP2t_1o= cutP2f_1o+1 # afterP2f_1o = NR-32 # afterP2t_1o = afterP2f_1o+17 #line 3 for (ix1=1;ix1<2;ix1++) getline ; branch=substr($0,0,300) countbranch=gsub(/^ *| *$/, "",branch) countbranch=gsub("A","",branch) countbranch=gsub("E","",branch) countbranch=gsub(/^ *| *$/, "",branch) #line 4 for (ix1=1;ix1<2;ix1++) getline ; #line 5 for (ix1=1;ix1<2;ix1++) getline ; #after filter mail_code=substr($0,57,1) checked_account=substr($0,37,12) print "searching for P creating data program : " ,checked_account print "\"",branch,"\"" print "\"",req_branch,"\"" print length(branch) print length(req_branch) print mail_code system (" pause \"\" ") #&&(mail_code=="P") if ((req_branch==branch)) { print "condition satisfied " if ((checked_account==old_checked_account)) { print start_cut , "I am start_cut inside equation " countera ++ print "checked_account" , checked_account print "old_checked_account",old_checked_account #start_cut=NR-(50*(countera-1))-4 } #&&(old_checked_account!="") if (checked_account!=old_checked_account) { print "Writing to Disk" system ("sed -n \""saved_record","saved_record+47"p\" "fn">>"q"") countera=1 } #saved_record=NR #if ((countera==1)) #{ #start_cut=NR-5 #end_cut=NR+48 #} #if ((countera>1)) #{ #start_cut=NR-(50*(countera-1))-4 #end_cut=NR-5 #} #print NR , "I am NR " #print start_cut , "I am start_cut " #print end_cut , "I am end_cut " print "saved_record " saved_record print "checked_account " , checked_account print "old_checked_account",old_checked_account saved_record=NR old_checked_account=checked_account old_mail_code=mail_code old_branch=branch system ("pause \"\" ") } } } END { if ( (customer!=0) || ((customer==0)&&(old_checked_account!="")) ) { start_cut=NR-53 if (old_checked_account=="") start_cut=1 end_cut=NR print NR , "I am NR " print countera , "I am countera " print start_cut , "I am start_cut " print end_cut , "I am end_cut " print q #system ("pause \"\"") #system ("sed -n \""start_cut","end_cut"p\" "fn">>"q"") system ("sed -n \""start_cut","end_cut"p\" "fn">>"q"") #system ("copy "FILENAME" "post_dir" ") #start_cut=NR-12 } if ((customer==0)&&(old_checked_account=="")) { print"No Data in the filter of : ",fn>> q } } |
|
|
||||
|
||||
|
|
|
|||
|
On 8/7/2012 2:49 PM, ehabaziz2001@gmail.com wrote:
> I am cutting required pages detemined with conditions . Is there a better solution under awk for sucj task ? Post a small program that demonstrates your problem along with some sample input and the expected output for that input and you will probably get someone to read your post and respond. Ed. |
|
|||
|
On Aug 8, 4:26*pm, Ed Morton <mortons...@gmail.com> wrote:
> On 8/7/2012 2:49 PM, ehabaziz2...@gmail.com wrote: > > > I am cutting required pages detemined with conditions . Is there a better solution under awk for sucj task ? > > Post a small program that demonstrates your problem along with some sample input > and the expected output for that input and you will probably get someone to read > your post and respond. > > * * *Ed. My data file is like this : customer account 0001 page1 branch :home1 ...... customer account 0001 page2 branch :home1 ...... Marker End of page2 customer account 0002 page1 branch :home2 ...... customer account 0002 page2 branch :home2 ...... Marker End of page2 customer account 0003 page1 branch :home3 ...... customer account 0004 page2 branch :home3 ...... Marker End of page2 I want to choose customers whose branch is home2 and cut them put them into a separate file like this : customer account 0002 page1 branch :home2 ...... customer account 0002 page2 branch :home2 ...... Marker End of page2 I am cutting pages using NR identity and sed to a different file like below but I have problem with customers whom have 2 pages how to buffer the first page before reading the new page of the same customer . My program compares new account with old account and if it is not the same they copy the page into a separate file using sed like this : system ("sed -n \""cut_from","cut_to"p\" "fn">>"q"") The whole program like this : ------------------------------ { legend=substr($0,38,18) if (legend =="BANK OF CYPRUS") { #line 3 for (ix1=1;ix1<2;ix1++) getline ; branch=substr($0,0,300) countbranch=gsub(/^ *| *$/, "",branch) countbranch=gsub("A","",branch) countbranch=gsub("E","",branch) countbranch=gsub(/^ *| *$/, "",branch) #line 4 for (ix1=1;ix1<2;ix1++) getline ; #line 5 for (ix1=1;ix1<2;ix1++) getline ; #before filter mail_code=substr($0,57,1) checked_account=substr($0,37,12) print "Processing P Program account : " , checked_account if ((branch~req_branch) && (mail_code=="P")) { print "mail_code : " , mail_code if (checked_account==old_checked_account) { countera ++ flag="Y" } if ((checked_account!=old_checked_account) ) { if ((old_checked_account!="")) { print NR , "I am NR " # print saved_record , "I am saved_record " #print "checked_account " , checked_account #print "old_checked_account",old_checked_account if (countera!=1) cut_from=saved_record-(50*(countera-2))-4 else cut_from=saved_record-4 cut_to=NR-5 print "checked_account " , checked_account > "m.txt" print "old_checked_account",old_checked_account> "m.txt" print countera , "I am countera "> "m.txt" print cut_from , "I am cut_from " > "m.txt" print cut_to , "I am cut_to "> "m.txt" system ("sed -n \""cut_from","cut_to"p\" "fn">>"q"") } countera=1 saved_record=NR old_checked_account=checked_account } } } } |
|
|||
|
On 8/14/2012 5:43 AM, happytoday wrote:
> On Aug 8, 4:26 pm, Ed Morton <mortons...@gmail.com> wrote: >> On 8/7/2012 2:49 PM, ehabaziz2...@gmail.com wrote: >> >>> I am cutting required pages detemined with conditions . Is there a better solution under awk for sucj task ? >> >> Post a small program that demonstrates your problem along with some sample input >> and the expected output for that input and you will probably get someone to read >> your post and respond. >> >> Ed. > > My data file is like this : > > customer account 0001 page1 > branch :home1 > ..... > customer account 0001 page2 > branch :home1 > ..... > Marker End of page2 > customer account 0002 page1 > branch :home2 > ..... > customer account 0002 page2 > branch :home2 > ..... > Marker End of page2 > customer account 0003 page1 > branch :home3 > ..... > customer account 0004 page2 > branch :home3 > ..... > Marker End of page2 > > I want to choose customers whose branch is home2 and cut them put them > into a separate file like this : > customer account 0002 page1 > branch :home2 > ..... > customer account 0002 page2 > branch :home2 > ..... > Marker End of page2 Why do you want the "Marker End of page2" line in your output file? Seems like that'd be irrelevant. It looks like all you need is something like this: BEGIN{ FS = ":" } /^customer/ { if (branch == "home2") { print data } data = sep = "" } /^branch/ { branch = $2 } { data = data sep $0; sep = ORS } END { if (branch == "home2") { print data } } Regards, Ed. > > > I am cutting pages using NR identity and sed to a different file like > below but I have problem with customers whom have 2 pages how to > buffer the first page before reading the new page of the same > customer . My program compares new account with old account and if it > is not the same they copy the page into a separate file using sed > like this : > system ("sed -n \""cut_from","cut_to"p\" "fn">>"q"") > > > > > > The whole program like this : > ------------------------------ > > { > legend=substr($0,38,18) > > if (legend =="BANK OF CYPRUS") > { > #line 3 > for (ix1=1;ix1<2;ix1++) getline ; > branch=substr($0,0,300) > countbranch=gsub(/^ *| *$/, "",branch) > countbranch=gsub("A","",branch) > countbranch=gsub("E","",branch) > countbranch=gsub(/^ *| *$/, "",branch) > > > #line 4 > for (ix1=1;ix1<2;ix1++) getline ; > > > > #line 5 > for (ix1=1;ix1<2;ix1++) getline ; > > #before filter > mail_code=substr($0,57,1) > > checked_account=substr($0,37,12) > > > print "Processing P Program account : " , checked_account > > if ((branch~req_branch) && (mail_code=="P")) > { > print "mail_code : " , mail_code > > if (checked_account==old_checked_account) > { > countera ++ > flag="Y" > } > > > if ((checked_account!=old_checked_account) ) > { > if ((old_checked_account!="")) > { > print NR , "I am NR " > # print saved_record , "I am saved_record " > #print "checked_account " , checked_account > #print "old_checked_account",old_checked_account > > if (countera!=1) > cut_from=saved_record-(50*(countera-2))-4 > else > cut_from=saved_record-4 > > cut_to=NR-5 > print "checked_account " , checked_account > "m.txt" > print "old_checked_account",old_checked_account> "m.txt" > print countera , "I am countera "> "m.txt" > print cut_from , "I am cut_from " > "m.txt" > print cut_to , "I am cut_to "> "m.txt" > system ("sed -n \""cut_from","cut_to"p\" "fn">>"q"") > > } > countera=1 > saved_record=NR > old_checked_account=checked_account > } > > } > > > > > } > } > |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|