LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-04-2014, 05:32 AM   #1
a.albreiki
LQ Newbie
 
Registered: Apr 2008
Posts: 27

Rep: Reputation: 0
Python: Extract some data from one text file


I have text file which is generated on daily base, I'm trying to write script in python to extract some data from that file. file structured is constant, and there are some Variables are changing in each new file mentioned with blue color. The Values with read color are static values.

input file

---------------------------------------------------------------------
THIS IS AUTO GENERATED MESSAGE
---------------------------------------------------------------------
TO: AABBAA KKLLC OMAANNNN TSBBBBI MESSAGE KABBAAA [OOMML]
FROM: MAAA MOONSOR

NOTIFICATION:
AHMED OCESS, BALL ON THE FOLLOWING VALUE EVENT:

SALIMTUDE: 6.6 MWP
ALITH: 10KM
DATE: 31 MAY 2014
KHLGIN TIME: 1154 UTC
OMAITUDE: 18.90SS
UAEGITUDE: 107.40GG
KAIATION: OFF THE OOBBCC

http://XX.JJ.COM.AL

NOTE: THIS IS AUTO GENERATED MESSAGE


the out file should be contains mixed data blue and red + XXXLLL as static value

==================================================

XXXLLL
OMAANNNN TSBBBBI

SAL: 6.6 MWP
ALI: 10KM
DAT: 31 MAY 2014
KHL TI: 1154 UTC
OMA: 18.90SS
UAE: 107.40GG
KAI: OFF THE OOBBCC



=======

Please any one could advice how can start to write this script.

Thanks a lot in advance.

Last edited by a.albreiki; 06-05-2014 at 01:18 AM.
 
Old 06-05-2014, 03:08 AM   #2
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
Quote:
Originally Posted by a.albreiki View Post
Please any one could advice how can start to write this script.
The same way one starts with any programming problem: break it down into pieces and work out how to do each piece.

You have to do some of the work yourself - it's the only way you'll learn. Tell us how you've broken it down and what you've tried to accomplish each piece.
 
Old 06-05-2014, 03:27 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
why python? probably perl would be better. So here are some parts:
possible command line args?
open input file (check name, existence ...)
parse file (check content, collect relevant info)
close input file
create and print output
 
Old 06-05-2014, 12:46 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by pan64 View Post
why python? probably perl would be better.
Now you've done it.

Save the example text file as "test.txt", and then, in the same directory, write and run the following Python file, which I just knocked up:

Code:
#!/usr/bin/env python


def main():
    print colored('XXXLL', Colors.Green)

    keys = set(['TO:', 'SALIMTUDE:', 'ALITH:', 'DATE:', 'KHLGIN TIME:',
                'OMAITUDE:', 'UAEGITUDE:', 'KAIATION:'])

    with open('test.txt') as f:
        for line in f:
            tokens = line.split()
            if len(tokens) and tokens[0] in keys:
                if tokens[0] == 'TO:':
                    name = ' '.join(tokens[3:5])
                    print colored(name, Colors.Blue)
                    print
                else:
                    left = tokens[0][:3]
                    red = colored(left, Colors.Red)
                    right = ' '.join(tokens[1:])
                    blue = colored(right, Colors.Blue)
                    print '{0}: {1}'.format(red, blue)


def colored(text, color):
    return '{0}{1}{2}'.format(color, text, Colors.End)


class Colors(object):
    Red = "\033[91m"
    Green = "\033[92m"
    Blue = "\033[94m"
    Cyan = "\033[96m"
    White = "\033[97m"
    Yellow = "\033[93m"
    Magenta = "\033[95m"
    Grey = "\033[90m"
    Black = "\033[90m"
    Default = "\033[99m"
    End = '\033[0m'


if __name__ == '__main__':
    main()
It works with Python 2.6 and 2.7.

Last edited by dugan; 06-09-2014 at 12:03 PM.
 
Old 06-09-2014, 11:30 AM   #5
a.albreiki
LQ Newbie
 
Registered: Apr 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ndc85430 View Post
The same way one starts with any programming problem: break it down into pieces and work out how to do each piece.

You have to do some of the work yourself - it's the only way you'll learn. Tell us how you've broken it down and what you've tried to accomplish each piece.

Thank you for your advice, I was trying different ways before i post the issue in the forum. and I preferred to start with good a logarithm recommended by python experts.

Thanks a lot..
 
Old 06-09-2014, 01:15 PM   #6
a.albreiki
LQ Newbie
 
Registered: Apr 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dugan View Post
Now you've done it.

Save the example text file as "test.txt", and then, in the same directory, write and run the following Python file, which I just knocked up:

Code:
#!/usr/bin/env python


def main():
    print colored('XXXLL', Colors.Green)

    keys = set(['TO:', 'SALIMTUDE:', 'ALITH:', 'DATE:', 'KHLGIN TIME:',
                'OMAITUDE:', 'UAEGITUDE:', 'KAIATION:'])

    with open('test.txt') as f:
        for line in f:
            tokens = line.split()
            if len(tokens) and tokens[0] in keys:
                if tokens[0] == 'TO:':
                    name = ' '.join(tokens[3:5])
                    print colored(name, Colors.Blue)
                    print
                else:
                    left = tokens[0][:3]
                    red = colored(left, Colors.Red)
                    right = ' '.join(tokens[1:])
                    blue = colored(right, Colors.Blue)
                    print '{0}: {1}'.format(red, blue)


def colored(text, color):
    return '{0}{1}{2}'.format(color, text, Colors.End)


class Colors(object):
    Red = "\033[91m"
    Green = "\033[92m"
    Blue = "\033[94m"
    Cyan = "\033[96m"
    White = "\033[97m"
    Yellow = "\033[93m"
    Magenta = "\033[95m"
    Grey = "\033[90m"
    Black = "\033[90m"
    Default = "\033[99m"
    End = '\033[0m'


if __name__ == '__main__':
    main()
It works with Python 2.6 and 2.7.
Quote:
Originally Posted by dugan View Post
Now you've done it.

Save the example text file as "test.txt", and then, in the same directory, write and run the following Python file, which I just knocked up:

Code:
#!/usr/bin/env python


def main():
    print colored('XXXLL', Colors.Green)

    line_number = 0

    keys = set(['TO:', 'SALIMTUDE:', 'ALITH:', 'DATE:', 'KHLGIN TIME:',
                'OMAITUDE:', 'UAEGITUDE:', 'KAIATION:'])

    with open('test.txt') as f:
        for line in f:
            tokens = line.split()
            if len(tokens) and tokens[0] in keys:
                if tokens[0] == 'TO:':
                    name = ' '.join(tokens[3:5])
                    print colored(name, Colors.Blue)
                    print
                else:
                    left = tokens[0][:3]
                    red = colored(left, Colors.Red)
                    right = ' '.join(tokens[1:])
                    blue = colored(right, Colors.Blue)
                    data = '{0}: {1}'.format(red, blue)
                    print data

            line_number += 1


def colored(text, color):
    return '{0}{1}{2}'.format(color, text, Colors.End)


class Colors(object):
    Red = "\033[91m"
    Green = "\033[92m"
    Blue = "\033[94m"
    Cyan = "\033[96m"
    White = "\033[97m"
    Yellow = "\033[93m"
    Magenta = "\033[95m"
    Grey = "\033[90m"
    Black = "\033[90m"
    Default = "\033[99m"
    End = '\033[0m'


if __name__ == '__main__':
    main()
It works with Python 2.6 and 2.7.

1- Thanks dugan for your reply, the code is working well, but there is misunderstanding, I was mean by the color only to mention the information should be in the output so it should be without colors function.

2 - Also I have one extra Question, I want the output to be in new file contains the extracted data can call ( ext_data.txt ).
3 - Some time the input file (test.txt) existing without required data so I want condition if there is no (extracted data) don't create the output file.

4 - also Can I add any Linux command in same python script after finishing the previous steps, such as mv test.txt ../archive/.

Thanks again,,
 
Old 06-09-2014, 02:04 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Hi a.albreiki. I think I've given you enough code at this point. I can get you started on how you can code the rest (yourself) though:

Quote:
2 - Also I have one extra Question, I want the output to be in new file contains the extracted data can call ( ext_data.txt ).
Code:
with open('ext_data.txt', 'w') as f:
    f.write('string')
Quote:
3 - Some time the input file (test.txt) existing without required data so I want condition if there is no (extracted data) don't create the output file.
Oh come on. That's just an if statement.

Code:
4 - also Can I add any Linux command in same python script after finishing the previous steps, such as mv test.txt ../archive/.
Yes you can execute arbitrary external commands. However, you can probably do what you want in straight Python, without the use of external commands. That specifically includes moving files into directories. Look into the following Python modules:

https://docs.python.org/2/library/subprocess.html
https://docs.python.org/2/library/os.html
https://docs.python.org/2/library/shutil.html

From now on, you will be expected to learn enough Python to use the information in this post, to remove the colors from the output, etc.

Or you can take pan64's recommendation and evaluate Perl.

Last edited by dugan; 06-09-2014 at 02:32 PM.
 
Old 06-09-2014, 02:11 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by a.albreiki View Post
Thank you for your advice, I was trying different ways before i post the issue in the forum. and I preferred to start with good a logarithm recommended by python experts.

Thanks a lot..
I don't see anything logarithmic in your I/O or the code offered. What do you mean? Do you need help with an equation not mentioned?
 
Old 06-09-2014, 02:15 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by szboardstretcher View Post
I don't see anything logarithmic in your I/O or the code offered. What do you mean?
He meant "algorithm", not "logarithm".
 
Old 06-10-2014, 12:17 AM   #10
a.albreiki
LQ Newbie
 
Registered: Apr 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dugan View Post
He meant "algorithm", not "logarithm".
Thanks dugan
Yes, I was mean algorithm..
 
Old 06-10-2014, 12:27 AM   #11
a.albreiki
LQ Newbie
 
Registered: Apr 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dugan View Post
Hi a.albreiki. I think I've given you enough code at this point. I can get you started on how you can code the rest (yourself) though:



Code:
with open('ext_data.txt', 'w') as f:
    f.write('string')


Oh come on. That's just an if statement.

Code:
4 - also Can I add any Linux command in same python script after finishing the previous steps, such as mv test.txt ../archive/.
Yes you can execute arbitrary external commands. However, you can probably do what you want in straight Python, without the use of external commands. That specifically includes moving files into directories. Look into the following Python modules:

https://docs.python.org/2/library/subprocess.html
https://docs.python.org/2/library/os.html
https://docs.python.org/2/library/shutil.html

From now on, you will be expected to learn enough Python to use the information in this post, to remove the colors from the output, etc.

Or you can take pan64's recommendation and evaluate Perl.

Thanks a lot for your help and advice ..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 10:52 AM
Python - How to get text file data into an array on python. golmschenk Programming 4 11-11-2013 09:15 AM
[SOLVED] Search text file for records in another text file and pull extra data over to new Adzrules Linux - Newbie 21 11-03-2012 11:53 AM
[SOLVED] Extract multiple lines of data from a text file. shawnamiller Programming 8 04-30-2010 11:46 AM
Extract certain text info from text file xmrkite Linux - Software 30 02-26-2008 11:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration