0

I have a CSV file as below but id like column 1 and 4 removed and the last row removed and and the speech marks removed then to export a new file to a UNC path. I also want the script to run from a UNC path too if at all possible. Can the new filename be the same as the original with the addition of _new?

Current File (file.txt):

"col1","col2","col3","col4","col5"
"col1","col2","col3","col4","col5"
"col1","col2","col3","col4","col5"
"col1","col2","col3","col4","col5"
"","","","","","","","","","","","","1111.00","11",""

Expected end result (file_new.csv):

col2,col3,col5
col2,col3,col5
col2,col3,col5
col2,col3,col5

The PowerShell script I tried to use is:

Import-Csv C:\temp\*.txt -Delimiter "," |
    Select Column2,Column3,Column5 |
    Export-Csv C:\Temp\New.txt -Delimiter "," -Encoding UTF8 -NoTypeInfo
2
  • This sounds more like an order than a question... Please show us what you tried so far. Edit your question and offer a minimal reproducible example. We are not a coding service ;) Also your description of the script doesn't match the "expected end result".. (column 2 still exists and column 4 is delete?) Commented Feb 12, 2019 at 10:26
  • Sorry if it sounded abrupt. I shall add more now. Commented Feb 12, 2019 at 10:28

1 Answer 1

3

As your file has no headers (or they are named col1..5) you'll have to either supply them

> Import-Csv file.txt -Header (1..5|%{"Column$_"}) |Select-Object Column2,Column3,Column5 -SkipLast 1

Column2 Column3 Column5
------- ------- -------
col2    col3    col5
col2    col3    col5
col2    col3    col5
col2    col3    col5

Or use the proper names:

> Import-Csv file.txt |Select-Object Col2,Col3,Col5 -SkipLast 1

col2 col3 col5
---- ---- ----
col2 col3 col5
col2 col3 col5
col2 col3 col5
Sign up to request clarification or add additional context in comments.

6 Comments

Alternatively, if they wanted to remove specific columns and keep everything else they'd use Select-Object * -Exclude col1, col4.
Thanks for this. I have just tried to add the export on the end: Import-Csv c:\temp\*.txt -Header (1..5|%{"Column$_"}) |Select-Object Column2,Column3,Column5 -SkipLast 1| Export-Csv -Path C:\Temp\test.txt -Delimiter "," -Encoding UTF8 -notypeinfo But get the error: Cannot perform operation because the path resolved to more than one file. This command cannot operate on multiple files. It also create the test.txt as blank
Import-Csv won't accept multiple files as the error msg clearly states. You'll need to process files with a gci first. Do you really want to combine several input files in one output?
Im only importing one file and then trying to export the columns I need which is the odd thing
If there is only one file and you know the name, use that name. If the name could be changing but there is only one use gci .\*.txt |%{ import-csv $_.FullName -Header (1..5|%{"Column$_"})} |Select-Object Column2,Column3,Column5 -SkipLast 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.