4

I am a beginner in Powershell. I have some queries. I am using Import-Csv cmdlet for a tab delimited text file that is generated from an old software. Below is an equivalent version of the delimited file. From the text file I have to filter those entries which has the destination->observability§bin as 1.

Time [s]       Offset_Angle        destination->observability§bin
0.00E+00     -0.20402611670286        0.00000000000000E+00
4.32E+02      0.21319658757004        0.00000000000000E+00
8.64E+02      0.26803683992125        0.00000000000000E+00
1.30E+03      2.67379011780784        1.00000000000000E+00
1.73E+03     -2.89704767087971        1.00000000000000E+00
2.16E+03     -2.93302157473406        1.00000000000000E+00

The code snippet is as below

$ap = Import-Csv -Delimiter "`t" -Path E:\Myfiles\textfile.txt

$ap|where-Object {$_.destination->observability§bin -eq "1.00000000000000E+00”}

I got the first line working. When I execute the second line it throws me an error message.

At line:1 char:34
+ $ap|where-Object {$_.destination->observability§bin
+                                  ~                                   
You must provide a value expression following the '-' operator.
At line:1 char:53
+ ... _.destination->observability§bin -eq '1.0000 ...
+                                      ~~~                                                              
Unexpected token '-eq' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

So i guess it takes the '-' sign in the input parameter as an operator(char 34). I also tried some escape characters like back tick(`) before the - sign. But didnt get anything. Please help me with this.

Thanks.

Saravana Murthy
  • 543
  • 2
  • 6
  • 16
  • The character between "observability" and "bin" is rendered as something weird in my environment. It doesn't look like a hyphen, and I don't think it's a legitimate symbol constituent. – Walter Mitty Oct 15 '15 at 10:46

3 Answers3

3

You should enclose it in quotes:

$ap | Where-Object {$_."destination->observability§bin" -eq "1.00000000000000E+00"}

Although you may also have issues with the § character

lalibi
  • 3,057
  • 3
  • 33
  • 41
2

Try using the -header clause of the import-csv cmdlet to provide your own names for the three fields, and name them something reasonable. You may have to program it to skip over the first record of the csv, in order to prevent the actual header from being read as if it were the first record.

Walter Mitty
  • 18,205
  • 2
  • 28
  • 58
0

Thanks all. Adding the name within quotes didnt work. It worked when I changed the header name to a legitimate one without a hyphen and any other special characters

Saravana Murthy
  • 543
  • 2
  • 6
  • 16
  • If you mark your own answer as correct, it will prevent others from trying to answer the question. That's ok in an instance like this one, where you posted the question legitimately, and got the answer later. – Walter Mitty Oct 19 '15 at 12:11