Results 1 to 10 of 16
I have been trying to do something for well over a month now but am totally stumped.
I'm simply trying to extract a section of a line to use as ...
- 01-11-2010 #1Just Joined!
- Join Date
- Jan 2010
- Posts
- 12
[SOLVED] At wits end trying to extract a portion of a line.
I have been trying to do something for well over a month now but am totally stumped.
I'm simply trying to extract a section of a line to use as a variable. Unfortunately, this line has numerous white spaces, I guess it needs escape characters, etc. and I have yet to find a reasonable formula that does this for me. I've been trying sed mostly because that's about all I am even vaguely familiar with on the linux side.
Here's the line I am trying to parse:
Murphy's law plays in here. There are numerous lines in the file withCode:user_pref("mail.server.server7.name", "CaneNet");
and so forth.Code:user_pref("mail.server.server7.name", user_pref("mail.server.server2.name", user_pref("mail.server.server13.name",
Theportion always varies but is known. I'll explain below:Code:"CaneNet");
What I am trying to extract and use as a variable is the 'server7' potion of the line. This line is always the same with 3 exceptions. They are the server number, the last part of the code and the whitespace betweenI am always going to know the last part of the line. In this case it'sCode:.name", and "CaneNet");
Unfortunately their may be one whitespace just prior toCode:"CaneNet"); Next time it may be "Fred"); or "Sally Doe");
Other times there may be 2 or 4, etc.Code:"CaneNet");
Whatever, my need is the server number and, unfortunately, that number is unknown and varies. It could be 2 or it could be as much as 25.
Uniquely, I am able to do this in MS-DOS easily utilizing find with tokens, etc. but it's not MS that I am dealing with
- 01-11-2010 #2Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
If it is just the numbers you need, this might be of help :
[ICODE]
echo "user_pref("mail.server.server7.name", "CaneNet")" | tr -cd [0-9]
[/CODE]
It won't suffice if your username contain numbers.
- 01-11-2010 #3
Sorry I can't help you with specifics as I don't know the language, but couldn't you create an array of some sort from each line's variables, then pull the 1st(2nd, 3rd, whatever you want) variable from each array that is built?
Aloof linux user #whatever.
I tested off the charts for MENSA. Unfortunately, it was off the wrong end of the chart.
- 01-11-2010 #4Just Joined!
- Join Date
- Jan 2010
- Posts
- 12
Well, the server number is all that I actually need to make a variable. The bug that bites with the above code is that the number portion of .server7. is the unknown that I am trying to obtain and that's the part I am in need of. That server number could be anywhere between 1 and 50 or even higher. The entire line is known except for that number.
Also, theres no guarantee that the usernames will not contain numbers.
What I am showing in bold is what I need. I do not know beforehand what that number is. That is what I am trying to discover to make a variable of id=server7 or id=server9 or id=server14, etc.
Code:echo "user_pref("mail.server.server7.name", "CaneNet")"
- 01-11-2010 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Sometimes it's just easier to use another language such as python, perl, java, C, or whatever to do this sort of parsing and return the string that you want. There are problems that a more general purpose language is better suited for than your normal bash/sed/awk srcipting tools. To paraphrase someone or other, "When your only tool is a hammer, all problems look like nails". You need more tools in your kit.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 01-11-2010 #6Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
Using grep with regular expressions can identify any lines with "serverXX" in it.
Then the cut command with -d can split the line into an array based on a . delimiter.Code:grep -G server[0-9] test_data.txt
That would seem to work in my mind.
- 01-11-2010 #7Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
This one has got limits too.Code:echo "user_pref("mail.server.server1.name", "CaneNet3")" | grep -o server[0-9] echo "user_pref("mail.server.server17.name", "CaneNet3")" | grep -o server[0-9][0-9]
(It seems that you are managing costly software over a cluster of servers...)
- 01-11-2010 #8Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
Just a friendly comment to Rubberman
That's true, some jobs need a full blown programming language. For this kind of job, I think it should be tackled in bash as it does not create supplementary dependencies, we don't need a compiler, a particular software setup... which would be an overkill for a problem which would require a few bash commands.You need more tools in your kit.
[Light moisture may suffice for the garden.
Perhaps this could be quoted in some years ! LOL ]
- 01-11-2010 #9Just Joined!
- Join Date
- Jan 2010
- Posts
- 12
I'm not sure you all are getting the jest of this and it's probably because I am not explaining it very well. This is not my forte.
Thanks for the input but just disregard this thread. I will just use a windows machine. I have a batch file I created that does this easily.
- 01-11-2010 #10Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
We all try to help our best as per information you give, if you retain information, threads like this one would be a waste of ressources. Fleeing to win as a gold standard is pointless in my view.because I am not explaining it very well
Yet, the server numerical suffix can still be retrieved :
Code:echo "user_pref("mail.server.server27.name", "CaneNet3")" | awk '{print $1}' | tr -cd [0-9]



