LinuxQuestions.org
Visit Jeremy's Blog.
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 09-08-2016, 12:39 PM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
python print string match from file but only second column


Hello all,

I'm writing a python script that searches standard parameter info from a config file so that it can find unique info to the right of the parameter. For instance here is my list:

Code:
SID 2861
RID 383
hostname server1
hostaddress server1.domain.com
ifcfg-eth0 192.168.2.100
eth0-netmask 255.255.255.0
gateway 192.168.2.1
ifcfg-eth1 192.168.1.100
eth1-netmask 255.255.255.0
lanes 6 1 2 3 4 5 31
xterminal no
macs yes
The first column is always the same per server and the second column changes.

I have found a way to match the line and print the info on the second column:

Code:
for line in open("ss2861.cfg"):
	if "SID" in line:
		SID = line[4:8]
		print SID
But this is a bad solution, because a lot of the parameters in the second column vary in length so it's not a good solution to just count how many characters are after the first parameter.

I've been trying to find a way to search the first parameter AND print the second column. So far I've only been able to find one or the other.

This split works, but now I have the whole second columns without having searched for the specific parameter:

Code:
with open('ss2861.cfg') as inf:
	for line in inf:
		parts = line.split()
		if len(parts) > 1:
			print parts[1]
I'm new to python and I know bash would be able to handle this with awk, but I'm trying to force myself to do this with python so I can learn another scripting tool.

If anyone can assist I'd be very grateful. If this is not enough info then please let me know. Thank you! Keith
 
Old 09-08-2016, 01:08 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Before assisting further, I would like to know what should be printed for the following line which clearly contains more than 2 fields:
Code:
lanes 6 1 2 3 4 5 31
I am assuming for the line to be unique for a server then any one of those numbers could change and not just the second column 6??
 
1 members found this post helpful.
Old 09-08-2016, 02:10 PM   #3
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Before assisting further, I would like to know what should be printed for the following line which clearly contains more than 2 fields:
Code:
lanes 6 1 2 3 4 5 31
I am assuming for the line to be unique for a server then any one of those numbers could change and not just the second column 6??
Yes, that is correct.

In the 'lanes' line, the second column is the total number of lanes (6) and the numbers following that are the sequence (1 through 5 and 31). Sometimes it's 6 lanes but a different sequence like 2 through 7 and other varieties. Also there can be more than just 6 lanes at any given server set up.

I should have pointed that out. My plan when writing this threat was to learn the two-column method first and then take that information to push further into the more complicated areas (Such as the 'lanes' line).

Thanks for looking at this, please let me know if I can provide any more info to make things easier. Keith
 
Old 09-08-2016, 03:27 PM   #4
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Before assisting further, I would like to know what should be printed for the following line which clearly contains more than 2 fields:
Code:
lanes 6 1 2 3 4 5 31
I am assuming for the line to be unique for a server then any one of those numbers could change and not just the second column 6??
Hello grail,

I finally found a solution to this:

Code:
for line in open("ss2861.cfg"):
	if "SID" in line:
		print line.split("SID ",1)[1]
Thanks for your assistance. If you have a more elegant way of doing this then please share. I can use the info. Keith
 
Old 09-08-2016, 09:19 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I probably would have the separator as a space, that way you get access to both the first and second items should they be needed. For your future idea of the 'lanes' if you use space you will then have
access to all columns in future indexes of the list created, once you remove the max_split number.
 
1 members found this post helpful.
Old 09-09-2016, 09:48 AM   #6
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I probably would have the separator as a space, that way you get access to both the first and second items should they be needed. For your future idea of the 'lanes' if you use space you will then have
access to all columns in future indexes of the list created, once you remove the max_split number.
This seems to do the trick better than what I previously posted:

Code:
for line in open("ss2861.cfg"):
	if "SID" in line:
		SID = line.split()[1]
                print SID
This might be the key to moving forward towards the next step. I'll be working on that today. Thanks again
 
Old 09-12-2016, 02:57 AM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by keif View Post
This seems to do the trick better than what I previously posted:

Code:
for line in open("ss2861.cfg"):
	if "SID" in line:
		SID = line.split()[1]
                print SID
This might be the key to moving forward towards the next step. I'll be working on that today. Thanks again
I think Python is a great language, but for this case I find it to be somewhat overkill. You can do this with a fairly simple sed:
Code:
sed -n 's/SID \([0-9].*$\)/\1/p' config.txt 
2861
Best regards,
HMW

Last edited by HMW; 09-12-2016 at 03:00 AM.
 
  


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 print a column starting with a string using awk? socalheel Programming 9 06-29-2015 07:18 PM
print column with a string using awk or grep bison72 Linux - Newbie 2 01-21-2015 10:31 PM
[SOLVED] python 2.7 unable to match against string szboardstretcher Programming 5 05-06-2013 05:54 PM
compare second column of a file then print the first column of it in a ne fil if true java_girl Linux - Newbie 2 03-16-2012 04:50 AM
[SOLVED] search a string and print column vikas027 Programming 6 03-14-2010 09:20 AM

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

All times are GMT -5. The time now is 01:35 PM.

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