aleph01

Clean up an array or text file

10 posts in this topic

#1 ·  Posted

Good evening (here - metro Atlanta, GA, USA:

I’m using “net view” from a command prompt to enumerate active nodes on a domain which I output to a .txt file.  I then read the lines in the file and add the computer names to an array.  All of that works fine.  However, net view also returns “remarks” on some of the computers, such as
 
\\computername        catalog   (for a library catalog only machine)

The format is always the same –“ \\computername “ (which is the form I want the data in), followed by some number of spaces and the “remark,” if there is one for that computer.

How can I either remove the remarks from the array or remove them from the text file, whichever is easier/more efficient so that I get a clean array of \\computernames only?

Thanks in advance for any help.  Code below.

#include <File.au3>
#include <Array.au3>
Local $n, $i, $file, $read, $size, $aArray[0]
RunWait(@ComSpec & " /c " & " net view > C:\Users\computerqueen\nodes.txt", "", @SW_HIDE)
$n = _FileCountLines("C:\Users\computerqueen\nodes.txt"); returns the number of lines
$file = FileOpen ("C:\Users\computerqueen\nodes.txt")
For $i = 1 to $n
    $read = FileReadLine ($file, $i)
    If StringInStr ($read, "\\") Then _ArrayAdd ($aArray, $read)    
Next
FileDelete ("C:\Users\computerqueen\nodes.txt")
$size = UBound ($aArray)
_ArrayInsert ($aArray, 0, $size)
_ArrayDisplay ($aArray)

By the way, I'm not computerqueen.  I can't help it, I was born hetero.  Before I arrived, the department was all female.  One of them must have been responsible for the name.

_aleph_


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites



#2 ·  Posted

can you give a small sample of the current array, and what you would like that array to look like.  There are many ways to go at this, best everyone have the same finish line rather than imagine their own


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#3 ·  Posted

I don't have a copy of the array.  Following is output from the text file.  The array populates one line per element.  I need those lines with data other than the computer name to have the extraneous data deleted.  There must be a command to use in a loop to delete the rest of the line beginning at the first space.

\\PR-LDS                                                                      
\\PRAMH1                                                                      
\\PRAMH3               PRAMH3                                                 
\\PRAMHSTAFF                                                                  
\\PRBRMGR                                                                     
\\PRCOLLSS                                                                    
\\PRFRIENDS                                                                   
\\PRINFOCAMVIEW                                                               
\\PRINFOD1                                                                    
\\PRINFOD2             PRINFOD2                                               
\\PRINFOD3                                                                    
\\PRINFOSUP                                                                   
\\PRINFOWK1                                                                   
\\PRINFOWK2                                                                                                              
\\PROPACS1                                                                    
\\PROPACS10                                                                   
\\PROPACS11                                                                   
\\PROPACS12                                                                   
\\PROPACS13            Catalog                                                
\\PROPACS14            Catalog                                                
\\PROPACS15            Catalog                                                
\\PROPACS16            Catalog                                                
\\PROPACS17            Catalog                                                
\\PROPACS18                                                                   
\\PROPACS2                                                                    
\\PROPACS3                              


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

#4 ·  Posted

you can make that wall code friendly next time. today, a gift.

#include<array.au3>

local $array[20]

$array[0] = "\\PR-LDS    "
$array[1] ="\\PRAMH1      "
$array[2] ="\\PRAMH3            PRAMH3"
$array[3] ="\\PRAMHSTAFF  "
$array[4] ="\\PRBRMGR    "
$array[5] ="\\PRCOLLSS  "
$array[6] ="\\PRFRIENDS   "
$array[7] ="\\PRINFOCAMVIEW  "
$array[8] ="\\PRINFOD1     "
$array[9] ="\\PRINFOD2             PRINFOD2"
$array[10] ="\\PRINFOD3     "
$array[11] ="\\PRINFOSUP   "
$array[12] ="\\PRINFOWK1       "
$array[13] ="\\PRINFOWK2       "
$array[14] ="\\PROPACS13            Catalog"
$array[15] ="\\PROPACS14            Catalog"
$array[16] ="\\PROPACS15            Catalog"
$array[17] ="\\PROPACS16            Catalog"
$array[18] ="\\PROPACS17            Catalog"
$array[19] ="\\PROPACS3    "

_ArrayDisplay($array)

For $i = 0 to ubound($array) -1
    $array[$i] = stringleft($array[$i] , StringInStr($array[$i] , " ") - 1)
Next

_ArrayDisplay($array)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#5 ·  Posted

Thanks, iamtheky, I'll give it a try tomorrow at work.

_aleph_


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

#6 ·  Posted

Give it a try:

#include <Array.au3>

Local $i, $file, $aArray

RunWait(@ComSpec & " /c " & " net view > C:\Temp\nodes.txt", "", @SW_HIDE)
$file = StringRegExp(FileRead('C:\Temp\nodes.txt'), '\\\\.*', 3)

For $i In $file
    $aArray &= StringStripWS(StringLeft($i, 23), 3) & @CR
Next
$aArray = StringSplit(StringTrimRight($aArray, 1), @CR)
FileDelete ("C:\Temp\nodes.txt")
_ArrayDisplay($aArray)

 


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

#7 ·  Posted

Or cull the input with the regexp...

#include <Array.au3>

RunWait(@ComSpec & " /c " & " net view > c:\k\temp\nodes.txt", "", @SW_HIDE)

$afile = StringRegExp(FileRead('c:\k\temp\nodes.txt'), '\\\\([^ ]+).*', 3)
If @error Then Exit MsgBox(17, 'No Matches', 'No matches returned from regexp', 5)

_ArrayDisplay($afile)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#8 ·  Posted

@kylomas thanks for sharing I knew there was a way to do it with RegEx I just couldn't figure out.


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Thanks, iamtheky, works like a charm.  Thanks everybody for their contributions.

Danny35d,

I don’t understand your use of StringRexExp, and I have a question, though:  In the line

$file = StringRegExp(FileRead('C:\Temp\nodes.txt'), '\\\\.*', 3)

Why is the second parameter ‘\\\\.*’ when the regular expression to match is “\\:” or “\\ ”?  The code works, I just don’t understand how.

Thanks,

_aleph_


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

#10 ·  Posted

Because \ is used as a mask to say the following character is meant literally you have to mask \ itself.

Example: A dot . means any character. But if you really mean a dot you must type \. So if you really mean a \ you have to type \\

For further information read help file about StringRegExp. Conrad


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now