Script on pattern matching and print lines and export to excel

Hi Friends,

I am working on a script.. Looking forward for your expert help.....

My requirement is:

I have a text file where, need to search equip * RTF or end of line with RTF ,once this pattern is found then print 2nd line, 6th line, 7th line to a different file.
For Ex:

equip 1 RTF
FULL
BCCH
2 0
0
4 0 4 6 9 106 1353
75
255 255 255 255 255 255 255 255
5 5 5 5 5 5 5 5
0
2
0
0
3
3
0
equip 2 RTF
FULL
NON_BCCH
0 1
0
4 0 4 6 9 106 1351
80
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0
0
2
1
0
1
0

OutPut will be

BCCH 4 0 4 6 9 106 1353 75
NON_BCCH 4 0 4 6 9 106 1351 80

so on.. Thr wil be many lines matching equip 1 RTF...

Regards
Shalini

what have you tried till now??
I will give you some hint..
use sed there is an option called "n" and "N" read man page and give it a try...

there're a number of similar threads - please use the Search function first next time you're about to open a new thread.

nawk 'c&&c-- {if (c==5 ||c==2||c==1) printf $0 ((c==1)?ORS:OFS);next} /^equip.*RTF$/ {c=7}' myFile

Thanks a lot... It did work after i use the below line

nawk 'c&&c-- {if (c==5 ||c==1||c==0) printf $0 ((c==0)?ORS:OFS);next} /^*RTF$/ {c=7}' BSC01_CHARKOP.COMBINED > aa1

But output is:

BCCH 4 0 4 6 9 106 1351 77
BCCH 4 0 4 6 9 106 1352 68
NON_BCCH 4 0 4 6 9 106 1351 87
NON_BCCH 4 0 4 6 9 106 1351 113
NON_BCCH 4 0 4 6 9 106 1351 122
NON_BCCH 4 0 4 6 9 106 1352 112
NON_BCCH 4 0 4 6 9 106 1352 119
NON_BCCH 4 0 4 6 9 106 1352 124
NON_BCCH 4 0 4 6 9 106 1352 756

I wanted something like this :

BCCH-4 0 4 6 9 106 1351-77
BCCH-4 0 4 6 9 106 1352-68
NON_BCCH-4 0 4 6 9 106 1351-87-113-122
NON_BCCH-4 0 4 6 9 106 1352-112-119-124

Thanks and Regards
Shalini

This is not what you wanted originally.
Please provide a sample input that resulted in that/desired output.
Also, what have you tried to do yourself to achieve the desired output?

Hi,

Input is same...

Now that this output is generated need to just delete the occurances..
Considering same output
But output is:

BCCH 4 0 4 6 9 106 1351 77
BCCH 4 0 4 6 9 106 1352 68
NON_BCCH 4 0 4 6 9 106 1351 87
NON_BCCH 4 0 4 6 9 106 1351 113
NON_BCCH 4 0 4 6 9 106 1352 112
NON_BCCH 4 0 4 6 9 106 1351 122
NON_BCCH 4 0 4 6 9 106 1352 119
NON_BCCH 4 0 4 6 9 106 1352 124
NON_BCCH 4 0 4 6 9 106 1352 756

If you just check 3rd 4th 6th only last values are varrying.... So what i want is in same line it should be appended

Like
Non_BCCH 4 0 4 6 9 106 1351 87 113 122

thats it... Considering the last values in same line...

What i tried was:

By using below input
contents of file.txt:
TCS,1
TCS,2
TCS,3
TCS,4
CTS,1
CTS,2
CTS,3

O/P:
TCS, 1 2 3 4
CTS, 1 2 3

But i am not sure that al values wil be in continuous manner

considering

abc,1
abc,2
xyz,5
abc,4
i am not getting
Below code
awk -F, '
{
if(NR == 1)
printf("%s",$0);
else
{
if($1 != var)
{
printf("\n%s,%d",$1,$2);
}
else
printf(" %s",$2);
}
var = $1;
}' file.txt

no, the output you're quoting was not produced from the previously quoted input.
Please provide the original input resulted in the quoted output.
I think I know what you want - just need a sample input to test.

Actually input is first thing what i have posted-----
equip 1 RTF
FULL
BCCH
2 0
0
4 0 4 6 9 106 1353
75
255 255 255 255 255 255 255 255
5 5 5 5 5 5 5 5
0
2
0
0
3
3
0
equip 2 RTF
FULL
NON_BCCH
0 1
0
4 0 4 6 9 106 1351
80
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0
0
2
1
0
1
0
equip 2 RTF
FULL
NON_BCCH
0 1
0
4 0 4 6 9 106 1352
80
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0
0
2
1
0
1
0
equip 2 RTF
FULL
NON_BCCH
0 1
0
4 0 4 6 9 106 1351
89
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0
0
2
1
0
1
0

So i have just split my requiremnts instead of directly getting used below line as suggested by you :

nawk 'c&&c-- {if (c==1||c==0) printf $0 ((c==0)?ORS:OFS);next} /^*RTF$/ {c=6}' myFile

then got output:

4 0 4 6 9 106 1353 75
4 0 4 6 9 106 1351 80
4 0 4 6 9 106 1352 80
4 0 4 6 9 106 1351 89

So now next task would be to consider this temp output and get the below format:

4 0 4 6 9 106 1353 75
4 0 4 6 9 106 1351 80 89
4 0 4 6 9 106 1352 80

Hope this clarifies.....

:slight_smile:

nawk '
   c&&c-- {
       if (c==1||c==0) { 
          if (c!=0) 
             idx=(idx)?idx OFS $0:$0
          else 
            a[idx]=(idx in a)?a[idx] OFS $0:$0
       }
       next
    } 
   /^*RTF$/ {c=6;idx=""} 
  END { 
    for (i in a) 
       print i OFS a
}' myFile

Hey this is coool.. Thanks a lot :)....

---------- Post updated at 07:07 PM ---------- Previous update was at 06:21 PM ----------

@vgersh99

------------------------------------------
nawk '
c&&c-- {
if (c==1||c==0) {
if (c!=0)
idx=(idx)?idx OFS $0:$0
else
a[idx]=(idx in a)?a[idx] OFS $0:$0
}
next
}
/^*RTF$/ {c=7;idx=""}
END {
for (i in a)
print i OFS a
[i]}' inputfile


Hey The above piece of code is working perfectly fine when want to retrieve on basis of one pattern i.e RTF condering the below input as same before, can you help me with in cases of two or three patterns for example :

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_hop_params cell_number = 4 0 4 6 9 106 1351
0
1
1
80 82 119
45
chg_hop_params cell_number = 4 0 4 6 9 106 1351
1
1
1
869 873
38

Considering this input, Output can we get it as below:

4 0 4 6 9 106 1311 2Ah 69 82 45 38
4 0 4 6 9 106 1312 3Ah 77

:))))......... The first group of numbers is from Key: *RTF ,2nd Key is BSIC, 3rd Key is same *RTF...

Is this possible...?????

Yes, it's possible - give it a shot.

Hey!!.. I have been trying but not succeeded... See the requirement: Its like need to print the data which is in same line as searching pattern........... Please help in this regard...

sed -n '/RTF/ {n;n;h;n;n;n;H;n;H;x;s/\n/ /g;p;}' yourfile

@summer_cherry

actually if you see the requirement its different.. RTF is already met, I also want to include BSIC and add_Cel value.......

So, can we conclude that you have got this from your original input

and now want your final output to be...

If yes...then probably we are close to helping you

@Rakesh
No actually...
Code:as quoted by you

4 0 4 6 9 106 1353 75
4 0 4 6 9 106 1351 80 89
4 0 4 6 9 106 1352 80
Is already met to get thm in this format using the below code
--------------------------------------

nawk '
c&&c-- {
if (c==1||c==0) {
if (c!=0)
idx=(idx)?idx OFS $0:$0
else
a[idx]=(idx in a)?a[idx] OFS $0:$0
}
next
}
/^*RTF$/ {c=7;idx=""}
END {
for (i in a)
print i OFS a
[i]}' inputfile

---------------------------------------
And consider this input 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_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

So if you see above code wil provide output as below:

4 0 4 6 9 106 1311 69 82
4 0 4 6 9 106 1312 77

But my requirement is :

4 0 4 6 9 106 1311 2AH 69 82 45 38
4 0 4 6 9 106 1312 3AH 77

Hope u got it , Let me know if any queries

Could you show exactly where you're stuck?

@ vgersh99

Just considering to retrieve 2 lines BSIC line and add_Cell line.

---------
nawk '
c&&c-- {
if (c==2 || c==0) {
if (c!=0)
idx=(idx)?idx OFS $0:$0
else
a[idx]=(idx in a)?a[idx] OFS $0:$0
}
next
}
/^add_cell*/ {c=2;idx=""}
END {
for (i in a)
print i OFS a
[i]}' inputfile

------------------------

This is printing only below output:

bsic = 2Ah
bsic = 3Ah

and not wid add_cell

here's something to start with:

nawk '
acell&&acell-- { if (acell==0) bsic[idx]=$NF }

c&&c-- {
    if (!c) {
       a[idx]=(idx in a)?a[idx] OFS $0:$0
       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=1 ;idx=$0}

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

Hey ya this is working for the BSIC part well.....

---------- Post updated at 06:17 PM ---------- Previous update was at 05:50 PM ----------

-------------------------------------

Thanks a lot for your support...I actually tested this code on my original file... Actually getting some extra data also which is not required.....

For ex:Considering the input i have provided i had chosen only required data... In the output for above code you wil fine this data have a look:

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_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

So output of your piece of code is :

4 0 4 6 9 106 1311 2Ah 69 82 1
4 0 4 6 9 106 1312 3Ah 77 2

Here 1 is not to be printed in our case..... Which is marked in red in inputFile as well as output