Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Argh... misread the problem. This will not solve it.
If you know python, you can probably use what's below as a guide, but will need to add more logic. If you don't know python, this is likely no help. At the end of the day, you just need to see if the call is of type "CallRec" & print out every line after the first ':' until you see a bunch of '*', then start over. I may try to come back & fix this script up, but for now, I'm going to go eat dinner.
Again, what's below is my initial response & does not address your problem correctly
Not too terribly difficult in python:
Code:
#!/usr/bin/python2.5
import sys
import string
class LQ_Parser(object):
def __init__(self,some_file):
"""
Parses a file, keying on the stuff before the ':'
"""
# read the file, storing each line in an array:
contents = open(some_file,'r').readlines()
# set up some containers:
self.data_dict = {} # this dictionary will allow one key for multiple vals
self.ret_string = "" # this string will hold the return value
for line in map(string.strip,contents):
parts = line.split(':')
if len(parts) < 2:
continue
key = parts[0]
val = string.join(parts[1:],':').strip()
if not self.data_dict.has_key(key):
self.data_dict[key] = [val]
else:
self.data_dict[key].append(val)
for dk in sorted(self.data_dict.keys()):
self.ret_string += "%s %s\n" % (dk,string.join(self.data_dict[dk],' '))
print self.ret_string
if __name__ == "__main__":
if len(sys.argv) < 2:
print >>sys.stderr, "\nMust give a file name.\n"
sys.exit(1)
LQ_Parser(sys.argv[1])
edited to add: I made the variables "self." so that they could be used in other functions...
I made the whole thing a class so that it could be easily used as a module later, in another application.
The thoery is you could make some other accessor function like "show_results" that returns the self.ret_string, then some other python app could do something like:
Code:
from parser import LQ_Parser
db_stuff = LQ_Parser(data_file).show_results()
for line in db_stuff.split('\n'):
insert_into_db(line)
data=open("file").read().split("\n\n")
print "CDR_TYPE callingNumber calledNumber answerTime releaseTime callDuration"
for items in data:
if "callingNumber" in items:
s=""
items=items.split("\n")
for i in items[1:-1]:
s=s+" "+i.split(": ")[-1]
print s
but one more question please, suppose i want to read in and print out only 3 fields, callingNumber, calledNumber and callDuration, where do i made decisions within the code you posted???
data=open("file").read().split("\n\n")
print "CDR_TYPE callingNumber calledNumber answerTime releaseTime callDuration"
for items in data:
if "callingNumber" in items:
s=""
items=items.split("\n")
for i in items[1:-1]:
s=s+" "+i.split(": ")[-1]
print s
data=open("file").read().split("\n\n")
print "CDR_TYPE callingNumber calledNumber answerTime releaseTime callDuration"
for items in data:
if "callingNumber" in items:
s=""
items=items.split("\n")
for i in items[1:-1]:
s=s+" "+i.split(": ")[-1]
print s
The code you highlighted is only the heading, and doesn’t actually do the “hard part” of your program. The relevant part is in the line:
Code:
for i in items[1:-1]:
You can change this to be any subsequence of the list items that you wish, either by index or by matching to your criteria (or you might even remove the offending elements before getting to this line).
P.S.
When quoting code (especially with spacing sensitive languages such as python, use “[code] Code goes here! [/code]” to preserve indentation.
P.P.S.
Just for fun, here is the original request in a perl one-liner:
stripping off other fields
is this possible using: import re
then use re.match() to filter off only needed fields?
Quote:
Originally Posted by osor
The code you highlighted is only the heading, and doesn’t actually do the “hard part” of your program. The relevant part is in the line:
Code:
for i in items[1:-1]:
You can change this to be any subsequence of the list items that you wish, either by index or by matching to your criteria (or you might even remove the offending elements before getting to this line).
P.S.
When quoting code (especially with spacing sensitive languages such as python, use “[code] Code goes here! [/code]” to preserve indentation.
P.P.S.
Just for fun, here is the original request in a perl one-liner:
but one more question please, suppose i want to read in and print out only 3 fields, callingNumber, calledNumber and callDuration, where do i made decisions within the code you posted???
data=open("file").read().split("\n\n")
print "CDR_TYPE callingNumber calledNumber answerTime releaseTime callDuration"
for items in data:
if "callingNumber" in items:
s=""
items=items.split("\n")
for i in items[1:-1]:
s=s+" "+i.split(": ")[-1]
print s
put Python codes in code tags as suggested by osor. Python uses indentation, so if i can't see indentation at relevant parts of your code, its hard to see what went wrong...
Code:
#!/usr/bin/env python
data=open("file").read().split("\n\n")
print "callingNumber calledNumber callDuration"
for items in data:
if "callingNumber" in items:
s=""
items=items.split("\n")
items=items[2:4]+items[-2:-1]
for i in items:
s=s+" "+i.split(": ")[-1]
print s
sys.exit()
exactly what i needed.. thanks again just for my sake, could you please brief on line:
items=items[2:4]+items[-2:-1]
cheers
Quote:
Originally Posted by ghostdog74
put Python codes in code tags as suggested by osor. Python uses indentation, so if i can't see indentation at relevant parts of your code, its hard to see what went wrong...
Code:
#!/usr/bin/env python
data=open("file").read().split("\n\n")
print "callingNumber calledNumber callDuration"
for items in data:
if "callingNumber" in items:
s=""
items=items.split("\n")
items=items[2:4]+items[-2:-1]
for i in items:
s=s+" "+i.split(": ")[-1]
print s
sys.exit()
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.