Script on pattern matching and print lines and export to excel


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script on pattern matching and print lines and export to excel
# 22  
Old 06-30-2009
Quote:
Originally Posted by vgersh99
Code:
nawk '
acell&&acell-- { if (!acell) bsic[idx]=$NF }
 
c&&c-- {
    if (c) v=$0
    else {
       if (NF>1) a[idx]=(idx in a)?a[idx] OFS v:v
       next
    }
}
$1=="add_cell" {
    acell=2;
    idx=""
    for(i=2;i<NF;i++) idx=(idx)? idx OFS $i : $i
    idxA[idx]
}
 
$0 in idxA { c=2 ;idx=$0;v=""}
 
END {
  for (i in idxA)
     print i OFS bsic[i] OFS a[i]
}' myFile


Hey this just cooool..... thnx a lot Smilie.....

---------- Post updated at 05:04 PM ---------- Previous update was at 01:42 PM ----------

Quote:
Originally Posted by vgersh99
Code:
nawk '
acell&&acell-- { if (!acell) bsic[idx]=$NF }
 
c&&c-- {
    if (c) v=$0
    else {
       if (NF>1) a[idx]=(idx in a)?a[idx] OFS v:v
       next
    }
}
$1=="add_cell" {
    acell=2;
    idx=""
    for(i=2;i<NF;i++) idx=(idx)? idx OFS $i : $i
    idxA[idx]
}
 
$0 in idxA { c=2 ;idx=$0;v=""}
 
END {
  for (i in idxA)
     print i OFS bsic[i] OFS a[i]
}' myFile

HI, This covers the second requirement also , now last thing would be to retrive last one value... from the data as below:

InputFile:
add_cell 4 0 4 6 9 106 1311 2
frequency_type = 1
bsic = 2Ah
wait_indication_parameters = 10
ccch_conf = 0
add_cell 4 0 4 6 9 106 1312 2
frequency_type = 1
bsic = 3Ah
wait_indication_parameters = 10
ccch_conf = 0
equip 1 RTF
FULL
BCCH
0 0
0
4 0 4 6 9 106 1311
69
255 255 255 255 255 255 255 255
equip 1 RTF
FULL
BCCH
1 0
0
4 0 4 6 9 106 1312
77
255 255 255 255 255 255 255 255
equip 1 RTF
FULL
NON_BCCH
0 2
0
4 0 4 6 9 106 1311
82
0 0 0 0 0 0 0 0
chg_element hopping_systems_enabled,0 1 2 cell_number = 4 0 4 6 9 106 1312
chg_element hopping_systems_enabled,1 1 2 cell_number = 4 0 4 6 9 106 1312
chg_element hopping_systems_hsn,0 45 2 cell_number = 4 0 4 6 9 106 1311
chg_element hopping_systems_hsn,1 38 2 cell_number = 4 0 4 6 9 106 1311

chg_hop_params cell_number = 4 0 4 6 9 106 1311
0
1
1
80 82 119
45
chg_hop_params cell_number = 4 0 4 6 9 106 1311
1
1
1
869 873
38

4 0 4 6 9 106 1311
1
1
4

4 0 4 6 9 106 1312
2
1
4

Output should be:
4 0 4 6 9 106 1311 2Ah 69 82 45 38
4 0 4 6 9 106 1312 3Ah 77


These 45 and 38 can be taken from any line which is given in red in above input file,on basis of hopping system or cell number

Really your help is appreciated...... Also plz expl me the logic behind the code to learn....
# 23  
Old 06-30-2009
Code:
nawk '
acell&&acell-- {
    if (!acell)  bsic[idx]=$NF
}

ahop&&ahop-- {
    if (!ahop) a[idx]=(idx in a)?a[idx] OFS $0:$0
}

c&&c-- {
    if (c) v=$0
    else {
       if (NF>1) a[idx]=(idx in a)?a[idx] OFS v:v
       next
    }
}
$1=="add_cell" {
    acell=2;
    idx=""
    for(i=2;i<NF;i++) idx=(idx)? idx OFS $i : $i
    idxA[idx]
}

$1 == "chg_hop_params" {
    split($0, hopA, " = ")
    idx=hopA[2]
    ahop=5
    next
}

$0 in idxA { c=2 ;idx=$0;v=""}

END {
  for (i in idxA)
     print i OFS bsic[i] OFS a[i]
}' myFile

# 24  
Old 06-30-2009
@vgresh99

Thanks a lots...... Really helpful for me......
# 25  
Old 07-08-2009
Data

Quote:
Originally Posted by vgersh99
Code:
nawk '
acell&&acell-- {
    if (!acell)  bsic[idx]=$NF
}
 
ahop&&ahop-- {
   if (!ahop) a[idx]=(idx in a)?a[idx] OFS $0:$0
}
 
c&&c-- {
    if (c) v=$0
    else {
       if (NF>1) a[idx]=(idx in a)?a[idx] OFS v:v
       next
    }
}
$1=="add_cell" {
    acell=2;
    idx=""
    for(i=2;i<NF;i++) idx=(idx)? idx OFS $i : $i
    idxA[idx]
}
 
$1 == "chg_hop_params" {
   split($0, hopA, " = ")
   idx=hopA[2]
   ahop=5
   next
}
 
$0 in idxA { c=2 ;idx=$0;v=""}
 
END {
  for (i in idxA)
     print i OFS bsic[i] OFS a[i]
}' myFile

I have a slight difference in the pattern:
Considering the same input as before:
InputFile:
Code:
add_cell 4 0 4 6 9 106 1311 2
frequency_type = 1
bsic = 2Ah
wait_indication_parameters = 10
ccch_conf = 0
add_cell 4 0 4 6 9 106 1312 2
frequency_type = 1
bsic = 3Ah
wait_indication_parameters = 10
ccch_conf = 0
equip 1 RTF
FULL
BCCH
0 0
0
4 0 4 6 9 106 1311
69
255 255 255 255 255 255 255 255
equip 1 RTF
FULL
BCCH
1 0
0
4 0 4 6 9 106 1312
77
255 255 255 255 255 255 255 255
equip 1 RTF
FULL
NON_BCCH
0 2
0
4 0 4 6 9 106 1311
82
0 0 0 0 0 0 0 0
chg_element hopping_systems_enabled,0 1 2 cell_number = 4 0 4 6 9 106 1312
chg_element hopping_systems_enabled,1 1 2 cell_number = 4 0 4 6 9 106 1312
chg_element hopping_systems_hsn,0 45 2 cell_number = 4 0 4 6 9 106 1311
chg_element hopping_systems_hsn,1 38 2 cell_number = 4 0 4 6 9 106 1311

chg_hop_params cell_number = 4 0 4 6 9 106 1311
0
1
1
82 119
45
chg_hop_params cell_number = 4 0 4 6 9 106 1311
1
1
1
869 873
38

4 0 4 6 9 106 1311
1
1
4

4 0 4 6 9 106 1312
2
1
4



Output should be:
4 0 4 6 9 106 1311-2Ah-69-82 119-45-868 867-38
4 0 4 6 9 106 1312-3Ah-77


Just to explain if too confusing...

< 4 0 4 6 9 106 1311> -- Cell Number our Unique key
<2Ah> -- BSIC
<69> --(Next string after the Key if 4th line from this would be BCCH)
<82 119> --4th Line of Chg_hop_params
<45> --5th Line of Chg_hop_params

Let me know if its going above head.... Looking forward for your help

Last edited by vgersh99; 07-08-2009 at 08:14 AM..
# 26  
Old 07-08-2009
Quote:
Originally Posted by shaliniyadav
I have a slight difference in the pattern:
Considering the same input as before:
InputFile:
Code:
add_cell 4 0 4 6 9 106 1311 2
frequency_type = 1
bsic = 2Ah
wait_indication_parameters = 10
ccch_conf = 0
add_cell 4 0 4 6 9 106 1312 2
frequency_type = 1
bsic = 3Ah
wait_indication_parameters = 10
ccch_conf = 0
equip 1 RTF
FULL
BCCH
0 0
0
4 0 4 6 9 106 1311
69
255 255 255 255 255 255 255 255
equip 1 RTF
FULL
BCCH
1 0
0
4 0 4 6 9 106 1312
77
255 255 255 255 255 255 255 255
equip 1 RTF
FULL
NON_BCCH
0 2
0
4 0 4 6 9 106 1311
82
0 0 0 0 0 0 0 0
chg_element hopping_systems_enabled,0 1 2 cell_number = 4 0 4 6 9 106 1312
chg_element hopping_systems_enabled,1 1 2 cell_number = 4 0 4 6 9 106 1312
chg_element hopping_systems_hsn,0 45 2 cell_number = 4 0 4 6 9 106 1311
chg_element hopping_systems_hsn,1 38 2 cell_number = 4 0 4 6 9 106 1311

chg_hop_params cell_number = 4 0 4 6 9 106 1311
0
1
1
82 119
45
chg_hop_params cell_number = 4 0 4 6 9 106 1311
1
1
1
869 873
38

4 0 4 6 9 106 1311
1
1
4

4 0 4 6 9 106 1312
2
1
4



Output should be:
4 0 4 6 9 106 1311-2Ah-69-82 119-45-868 867-38
4 0 4 6 9 106 1312-3Ah-77


Just to explain if too confusing...

< 4 0 4 6 9 106 1311> -- Cell Number our Unique key
<2Ah> -- BSIC
<69> --(Next string after the Key if 4th line from this would be BCCH)
<82 119> --4th Line of Chg_hop_params
<45> --5th Line of Chg_hop_params

Let me know if its going above head.... Looking forward for your help
I don't understand how you got '119-45-868 867-38' from the given sample input.
Also, what changes have you tried yourself? We're on the THIRD page of this thread - you should be able make some minor modifications to the code.
# 27  
Old 07-08-2009
Quote:
Originally Posted by vgersh99
I don't understand how you got '119-45-868 867-38' from the given sample input.
Also, what changes have you tried yourself? We're on the THIRD page of this thread - you should be able make some minor modifications to the code.
Im sorry about that...
Output would be:

Output should be:
4 0 4 6 9 106 1311-2Ah-69-82 119-45-869 873-38
4 0 4 6 9 106 1312-3Ah-77


are from the below lines in the input:

chg_hop_params cell_number = 4 0 4 6 9 106 1311
0
1
1
82 119
45
chg_hop_params cell_number = 4 0 4 6 9 106 1311
1
1
1
869 873
38
# 28  
Old 07-08-2009
something to start with - good luck!
Code:
BEGIN {
  SEPdash="-"
}
acell&&acell-- {
    if (!acell)  bsic[idx]=$NF
}

ahop&&ahop-- {
    if (ahop==1) hopv=$NF
    if (!ahop) a[idx]=(idx in a)?a[idx] OFS hopv SEPdash $0:hopv SEPdash $0
}

c&&c-- {
    if (c) v=$0
    else {
       if (NF>1) a[idx]=(idx in a)?a[idx] SEPdash v:v
       next
    }
}
$1=="add_cell" {
    acell=2;
    idx=""
    for(i=2;i<NF;i++) idx=(idx)? idx OFS $i : $i
    idxA[idx]
}

$1 == "chg_hop_params" {
    split($0, hopA, " = ")
    idx=hopA[2]
    ahop=5
    next
}

$0 in idxA { c=2 ;idx=$0;v=""}

END {
  for (i in idxA)
     print i SEPdash bsic[i] SEPdash a[i]
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print lines after matching two pattern

would like to print everything after matching two patterns AAA and BBB. output : CCC ZZZ sample data : AAA BBB CCC ZZZ (4 Replies)
Discussion started by: jhonnyrip
4 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Want to print out lines with a matching pattern from file

Hi all, I want to search for strings in file1 that can be found in file2 and print out the whole line when matching pattern is found. I have used the below command, but this is not working for me, because it is writing out only the matching patterns from file2, not the whole line. fgrep -o... (2 Replies)
Discussion started by: MonikaB
2 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

How to print all the lines after pattern matching?

I have a file that contains... Number -------------------- 1 2 3 4 i want to print all the numbers after the hyphen ... (6 Replies)
Discussion started by: ankitknit
6 Replies

6. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

7. Shell Programming and Scripting

perl script print the lines between two pattern

i have a file as below sample.pl parameter1 argument1 argument2 parameter2 I want out as below argument1 argument2 that is , i want to print all the lines between parameter1 & parameter 2. i tried with the following if($mystring =~ m/parameter1(.*?)parameter2/) (2 Replies)
Discussion started by: roopa
2 Replies

8. Shell Programming and Scripting

I want to print next 3 lines after pattern matching.

Dear Experts, I have file called file1 in which i am greping a pattern after that i want to next 3 lines when that pattern is matched. Ex:- file1 USA UK India Africa Hello Asia Europe Australia Hello Peter Robert Jo i want to next 3 lines after matching Hello... (12 Replies)
Discussion started by: naree
12 Replies

9. Shell Programming and Scripting

Perl script to match a pattern and print lines

Hi I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9. I appreciate any help with code Thanks Ammu (6 Replies)
Discussion started by: ammu
6 Replies

10. Shell Programming and Scripting

Print block of lines matching a pattern

Hi :), I am using the script to search "MYPATTERN" in MYFILE and print that block of lines containing the pattern starting with HEADER upto FOOTER. But my problem is that at some occurrence my footer is different e.g. ";". How to modify the script so that MYPATTERN between HEADER and different... (1 Reply)
Discussion started by: vanand420
1 Replies
Login or Register to Ask a Question