Avatar of whowasthat
whowasthatFlag for Canada

asked on 

Powershell: read a csv and look for keywords

Hello,

i have a sharepoint export of all our lists and i need to search for the sharepoint site (sharepoint.company.com) and write that entire line out to the a new file.  for example

 Content Type,         Title,                                               Encoded Absolute URL,            Select
test                            sharepoint.company.com       sharepoint.company.com       test      

the problem is i need to only find rows that have sharepoint.company.com anywhere BUT "Encoded Absolute URL"
when i do a search now, every row is returned because i am always finding sharepoint.company.com under Encoded Absolute URL

also, the column names keep changing depending on the csv im importing.  i need to find all instances of "sharepoint.company.com" under all columns except for Encoded Absolute URL

can anyone help?

thanks
Powershell

Avatar of undefined
Last Comment
Qlemo
Avatar of Qlemo
Qlemo
Flag of Germany image

Since you cannot provide something really reliable in format, I would check for two occurrences of sharepoint.company.com in each line. Something like
get-content c:\inputfile.txt | select-string '.*(sharepoint\.company\.com).*(sharepoint\.company\.com).*'  | out-file C:\outputfile.txt

Open in new window

Avatar of whowasthat
whowasthat
Flag of Canada image

ASKER

I could not get your script to copy any results. is it because im using a CSV
I found a way to delete the "Encoded Absolute URL" so im passed that problem.

right now i have many csv files with all different column names.  i need to go through each row and when i see "sharepoint.company.com" i need to write that to a new file.

we are doing a migration, and i need to find all our old domains
Avatar of Qlemo
Qlemo
Flag of Germany image

If you are able to remove that column, just treat the CSV files as text files, and apply select-string with 'sharepoint.company.com'.
Avatar of whowasthat
whowasthat
Flag of Canada image

ASKER

thank you.
but i seem to lose the formatting, its just one string.  is there a way to keep it a CSV?
Avatar of whowasthat
whowasthat
Flag of Canada image

ASKER

I've requested that this question be deleted for the following reason:

its the wrong question
Avatar of Qlemo
Qlemo
Flag of Germany image

If you cannot use the suggestion, you should not accept it. Does it work for you?
$contents = import-csv -Path I:\downloads\testannouncements.csv
$myobject = @()
foreach($content in $contents){
    foreach($inner in $content)
        {    
    if($inner -match 'sharepoint.com')
         {
         $myobject += $content       
        }
        }
    }
    
$myobject | Export-Csv output.csv -NoTypeInformation

Open in new window

Avatar of Qlemo
Qlemo
Flag of Germany image

If you need to search for a single occurance only
get-content c:\inputfile.txt | select-string 'sharepoint.company.com' -SimpleMatch  | out-file C:\outputfile.txt

Open in new window

As the first suggestion, it does not change the file format (different from making sure it is Unicode (UTF-16)). The original format should be kept, and the result is still a CSV file if the source has been.
Qlemo: the problem is that the headers are not output using your script
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo