Simulating awk in or equivalent in Select-String

This topic contains 5 replies, has 3 voices, and was last updated by  George Eldon Sullivan 2 years, 8 months ago.

  • Author
  • #30732

    Hey all, can someone help this old Linux Admin to convert a command to powershell syntax? The part I need help with is the "awk" part at the end. I am just putting in the Linux command to hold the place. Basically I am getting the Name from nslookup and want to strip off "Name:" so that I am left with the FQDN. Example:
    Name: and then have only

    nslookup "" | Select-String "Name:" | awk '{print $2}'

    Eventually I will do a massive nslookup, strip out the FQDN, and use the resulting list of names to perform an admin function remotely on our network.

  • #30733

    Bob McCoy
    ((nslookup | Select-String "^name:") -split ":\s+")[1]
  • #30734

    Curtis Smith

    You can accomplish this using -split.

    ((nslookup "" | Select-String "Name:") -split " +")[1]

    The above:
    1) Does the nslookup
    2) Pipes results to Select-String which uses RegEx to find the string containing "Name:"
    3) 1 and 2 are enclosed in parenthesis so that they process before the -split operator takes affect
    4) The -split operator uses RegEx to split the string into an array based off of 1 or more space characters. This results in an array with 2 elements.
    5) Then we put that in parenthesis so that 1-4 process before we get the value of the element in position 1.

    Hope that makes since.

  • #30735

    Bob, Curtis: Thanks. yes it makes perfect since. I failed to tell you I was doing my basic testing at the PS command line so I am getting an "Expressions are only allowed as the first element of a pipeline." I'll try it in a basic script to see if that makes a difference.

  • #30736

    Uhhh... Nevermind. Old habits are hard to break. I was actually, blindly, throwing in a | symbol.

  • #30737

    To be clear a | symbol before the split syntax not the one before Select-String. 🙂

You must be logged in to reply to this topic.