Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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:

    Code:
    user_pref("mail.server.server7.name", "CaneNet");
    Murphy's law plays in here. There are numerous lines in the file with

    Code:
    user_pref("mail.server.server7.name",
    
    user_pref("mail.server.server2.name",
    
    user_pref("mail.server.server13.name",
    and so forth.

    The
    Code:
    "CaneNet");
    portion always varies but is known. I'll explain below:

    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 between
    Code:
    .name",   and   "CaneNet");
    I am always going to know the last part of the line. In this case it's
    Code:
    "CaneNet");  Next time it may be "Fred");  or "Sally Doe");
    Unfortunately their may be one whitespace just prior to
    Code:
     "CaneNet");
    Other times there may be 2 or 4, etc.

    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

  2. #2
    Linux 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.

  3. #3
    Linux Newbie schwim's Avatar
    Join Date
    May 2005
    Location
    Denial
    Posts
    145
    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.

  4. #4
    Just Joined!
    Join Date
    Jan 2010
    Posts
    12
    Quote Originally Posted by nmset View Post
    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.

    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")"

  5. #5
    Linux Guru Rubberman's Avatar
    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!

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Using grep with regular expressions can identify any lines with "serverXX" in it.

    Code:
    grep -G server[0-9] test_data.txt
    Then the cut command with -d can split the line into an array based on a . delimiter.

    That would seem to work in my mind.

  7. #7
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    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]
    This one has got limits too.

    (It seems that you are managing costly software over a cluster of servers...)

  8. #8
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Just a friendly comment to Rubberman

    You need more tools in your kit.
    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.
    [Light moisture may suffice for the garden.
    Perhaps this could be quoted in some years ! LOL ]

  9. #9
    Just 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.

  10. #10
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    because I am not explaining it very well
    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.

    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]

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...