5

i have a CSV file :

COL1;COL2;COL3;COL4
1;1;;4
6;9;;0
9;8;;4

How do i fill in COL3 with a default value X?

so the result would be :

COL1;COL2;COL3;COL4
1;1;x;4
6;9;x;0
9;8;x;4

How can i achieve this using Powershell V2 or even ultra edit or Notepad++

Thanks

4
  • Use my FOSS tool CSVfix at code.google.com/p/csvfix - makes doing stuff like this a breeze. Commented Jul 28, 2010 at 9:22
  • Thanks, i m trying it, but i m dealing with special CSV files, with double pipe delimiters || and with more than 500 000 lines.. So i tried this command with your tool : echo -sep '||' myfile.csv ... but it doesnt work, it just keeps hanging (file too big?) Commented Jul 28, 2010 at 9:36
  • CSV as its name suggests is COMMA separated - what you have is not CSV. However, CSVfix can deal with other separators, but not multiple separator characters. If that's your format (why??) why didn't you use it in your example above? Also, you probably want to use double quotes to quote pipe symbols. Commented Jul 28, 2010 at 9:58
  • 1
    Actually, the C in CSV stands for Character, not Comma. So what the OP has is CSV, and your tool only deals with the most common format (using commas as the delimeter). Commented Aug 11, 2010 at 15:05

1 Answer 1

7
Import-CSV -Path "input.csv" -Delimiter ';' | `
ForEach-Object { $_.COL3 = "x"; return $_ } | `
Export-CSV -Path "output.csv" -Delimiter ';' -NoTypeInformation
Sign up to request clarification or add additional context in comments.

4 Comments

Instead of ; return $_, ; $_ is sufficient.
I know, but it's a better indication of what I mean.
yes, but it's generally confusing to people new to the language. as a guideline, consider using return only without arguments, and only to exit early. frankly, I think the keyword should have been designed like that in the first place.functions/scriptblocks early ony.
I do not understand the return $_, why is this required?

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.