Hi , I have a billing CDR file which has repeated lines as indicated below and I need to extract data between two strings (i.e.: <?> and </?>). Eventually, map that information with the corresponding field. I'm new to unix, any help will be greatly appreciated.
Gamini
Input (single line):
! TICKET NBR : 78 ! 3100.2.13.1 0.30 ! 3100.2.14.2 51 ! 1.2.1.8 <A>91368599971</A><B>Mobility</B><C>9138599971</C><D>9134284050</D><E></E><F>0220_KANSAS</F><G>NPA_913</G><H>402404</H><I>2002</I><J>12/03/2010 10:08:20</J><Q>A18</Q><R>P_30_LOCAL</R><K>RL 0.30/60/60 QL 60 CL 0.30 </K><L>0.30</L><M>RL 0.00/1/1 QL 60 CL 0.00 </M><N>0.00</N><O>0</O><P>9.70</P><P1></P1><TA>0</TA><MS>302614168599971</MS><P2></P2><BRQ></BRQ> ! 3100.2.984.45 0 !
Output:
Field A: 91368599971
Filed B: Mobility
Field C: 9138599971
.
.
Field MS: 302614168599971
Field P2:<empty>
Field BRQ: <empty>
Hello, jaygamini:
With your sample data in file named "data":
$ sed 's/[^<]*<\([^>]*\)>\([^<]*\)<\/\1>[^<]*/Field \1: \2</g;y/</\n/' data
Field A: 91368599971
Field B: Mobility
Field C: 9138599971
Field D: 9134284050
Field E:
Field F: 0220_KANSAS
Field G: NPA_913
Field H: 402404
Field I: 2002
Field J: 12/03/2010 10:08:20
Field Q: A18
Field R: P_30_LOCAL
Field K: RL 0.30/60/60 QL 60 CL 0.30
Field L: 0.30
Field M: RL 0.00/1/1 QL 60 CL 0.00
Field N: 0.00
Field O: 0
Field P: 9.70
Field P1:
Field TA: 0
Field MS: 302614168599971
Field P2:
Field BRQ:
It can also be done without the y command kludge (which along with the rest of the regular expression assumes that '<' does not appear outside of the tag markup elements themselves), but note that the newline in the substitution text must be immediately preceded by a backslash and inside stong single quotes:
sed 's/[^<]*<\([^>]*\)>\([^<]*\)<\/\1>[^<]*/Field \1: \2\
/g' data
Regards,
Alister
You are best. I really appreciate your help ...
If the input data is in a single line, then -
$
$
$ cat -n f7
1 ! TICKET NBR : 78 ! 3100.2.13.1 0.30 ! 3100.2.14.2 51 ! 1.2.1.8 <A>91368599971</A><B>Mobility</B><C>9138599971</C><D>9134284050</D><E></E><F>0220_KANSAS</F><G>NPA_913</G><H>402404</H><I>2002</I><J>12/03/2010 10:08:20</J><Q>A18</Q><R>P_30_LOCAL</R><K>RL 0.30/60/60 QL 60 CL 0.30 </K><L>0.30</L
><M>RL 0.00/1/1 QL 60 CL 0.00 </M><N>0.00</N><O>0</O><P>9.70</P><P1></P1><TA>0</TA><MS>302614168599971</MS><P2></P2><BRQ></BRQ> ! 3100.2.984.45 0 !
$
$ perl -lne 'while(/<(.*?)>(.*?)<\/.*?>/g){print "Field $1 : $2"}' f7
Field A : 91368599971
Field B : Mobility
Field C : 9138599971
Field D : 9134284050
Field E :
Field F : 0220_KANSAS
Field G : NPA_913
Field H : 402404
Field I : 2002
Field J : 12/03/2010 10:08:20
Field Q : A18
Field R : P_30_LOCAL
Field K : RL 0.30/60/60 QL 60 CL 0.30
Field L : 0.30
Field M : RL 0.00/1/1 QL 60 CL 0.00
Field N : 0.00
Field O : 0
Field P : 9.70
Field P1 :
Field TA : 0
Field MS : 302614168599971
Field P2 :
Field BRQ :
$
$
tyler_durden