LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-11-2017, 10:06 AM   #1
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Rep: Reputation: Disabled
Are you able to do a multple .split in python 2.6 ??


long story short im looking at the 4th field on a file which usually contains either one of the 2 below
a) abcd1234[1234]
b) abcd1234:

my question is is there a way to do the .split function with multiple varbs so instead of .split('[')[1] whats the correct way to do multiple.

Ive tried a couple of things and searched around but no luck

variable i question is processcorrected i want it to be either [ or :

Code:
#!/usr/bin/python
file = open('/var/log/messages')
for line in file:
	word = line.split(' ')
	date = word[0]
	server = word[3]
        process = word[4]
        processcorrected = process.split('[')[1]
	print "time is %s, server is %s" % (date, server)
 
Old 05-11-2017, 12:45 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
I don't think so. Personally I would follow the logic and use a simple if command (English-language logic follows; only works if you are guaranteed that there will be either a '[' or ':', otherwise it will need tweaking):

Code:
if there is a '[' in the string, set split char to '[', else set split char to ':'

run split using split char
 
Old 05-11-2017, 01:06 PM   #3
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
If I understand correctly "re.split()" will do what you want:https://docs.python.org/3/library/re.html#re.split
Code:
processcorrected = re.split('\[|:', process)[1]
 
1 members found this post helpful.
Old 05-18-2017, 03:40 AM   #4
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by norobro View Post
If I understand correctly "re.split()" will do what you want:https://docs.python.org/3/library/re.html#re.split
Code:
processcorrected = re.split('\[|:', process)[1]
Exactly what i was looking for !!!!! thanks very much

marking as solved
 
  


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
python using splitlines or split? ANU Programming 2 12-03-2014 04:13 AM
[SOLVED] Strange return from python split() function sharky Programming 3 06-26-2014 04:15 PM
[SOLVED] [PYTHON] Split a string by every nth characters recuse44 Programming 4 01-20-2011 03:34 AM
Split header from data in file using python pwc101 Programming 13 06-16-2009 07:27 PM
Renaming multple files.... maginotjr Slackware 4 06-04-2006 10:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:12 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