Pulling first 17 characters from line

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5045

    by millerlw at 2012-08-24 10:18:01

    Hi,
    I am new to PowerShell, but am making good use of it so far and working on another script to make my job easier. I need to read a text file, pull the first 17 characters from each line(which are MAC address), and put them in another text file behind other text. Or, just add new text BEFORE the 17 characters(MAC addresses) and delete everything after those characters. Here is a sample:

    00:21:a0:e2:9b:d5 Alert 1 0 Thu Aug 23 12:05:49 2012
    00:21:a0:e2:9b:dd Alert 1 0 Thu Aug 23 12:20:18 2012
    00:24:c4:1b:3d:43 Alert 3 0 Thu Aug 23 12:15:52 2012

    I want to add this "rogue ap classify friendly state external" before the MAC addresses and remove everything AFTER the MAC addresses.

    I know I can use get-content to read the file, and I think foreach-object to get the first 17 characters, but cannot figure out the code to get it done.
    The finished file will look like this
    rogue ap classify friendly state external 00:21:a0:e2:9b:d5
    rogue ap classify friendly state external 00:21:a0:e2:9b:dd
    rogue ap classify friendly state external 00:24:c4:1b:3d:43

    Or like I said, just add the new text before the MACs and remove everything after, if that's easier than creating another file and writing it all to the new file.
    Currently I do this by hand several times a week, and there are usually hundreds of entries each time I do it, very time consuming and boring.

    Any ideas?

    Thanks in advance,
    Leonard

    by willsteele at 2012-08-24 12:25:59

    I hear a lot of various requests. To add data before the MAC Addresses in the first set, you can use the -replace operator with a regular expression anchoring on the start of line with the ^ character. So, let's say you wanted to add code monkey to your first set, you could use this:

    '00:21:a0:e2:9b:d5 Alert 1 0 Thu Aug 23 12:05:49 2012' -replace "^", "Code Monkey "
    Code Monkey 00:21:a0:e2:9b:d5 Alert 1 0 Thu Aug 23 12:05:49 2012

    Taking it a step further, try this to get it all done,

    ('00:21:a0:e2:9b:d5 Alert 1 0 Thu Aug 23 12:05:49 2012' -replace "^", "rogue ap classify friendly state external ") -replace " Alert.+",""
    rogue ap classify friendly state external 00:21:a0:e2:9b:d5

    You could do the same thing with a foreach loop by reading it this way:

    Get-Content C:\file.txt |
    ForEach-Object {
    ($_ -replace "^", "rogue ap classify friendly state external ") -replace " Alert.+",""
    }

    I haven't tested with a file, but, it looks right. Give it a shot.

    by poshoholic at 2012-08-24 12:28:40

    You're on the right track with Get-Content and ForEach-Object. Assuming your file is called C:\test.txt, you should start with something like this:

    [script=powershell]Get-Content -LiteralPath C:\test.txt | ForEach-Object {
    # This is where you will convert the lines of text into a format you want.
    # $_ refers to the current line that you just read in (or $PSItem if you prefer and if you are using PowerShell 3.0 or later)
    }[/script]
    When you are working with strings, and you know you always want the first 17 characters, you can use the range operator to specify the portion you want, like this:

    [script=powershell]# Note we're using 0 to 16, not 1 to 17, because PowerShell arrays are 0-based, not 1-based
    $macAddress = $_[0..16][/script]
    Once you have that, you can return the strings you want with simple concatenation, like this:

    [script=powershell]'rogue ap classify friendly state external ' + $macAddress[/script]
    Put all of this together and you end up with this script:

    [script=powershell]Get-Content -LiteralPath C:\test.txt | ForEach-Object {
    $macAddress = $_[0..16]
    'rogue ap classify friendly state external ' + $macAddress
    }[/script]

    by poshoholic at 2012-08-24 12:30:27

    I didn't mean to double-answer this. It seems we both replied around the same time. Still, the result shows what PowerShell forums usually show: there are usually many ways to solve problems with PowerShell. Either of these methods should work.

    by millerlw at 2012-08-24 13:47:01

    You guys are great. I added 2 lines to assign the new file to write the string, assigned the new string to a variable, then write it to a new file. May be primitive, but it's what I know,

    $outFile = "c:\Docs\Powershell\Rogues\rogues.txt"
    Add-Content $outFile $newString

    Thanks so much for your help, this will save me a lot of work,
    Leonard

You must be logged in to reply to this topic.