Question

thing like this, i would like to import text file data into access. The data content example like this: (example.txt)

ID | Name | Message | User
----------------------------
1 | Joe | This is a text, error message | admin
2 | Mary | No error message, ID number: 9182734 | normal-user
3 | Terry | This is a long text, long message. Product ID : 1234,
Tag ID : fhdj123, Please restart | admin

I use -Delimiter "|" as a separator, and it works well for ID 1 and ID 2, but when comes to ID 3, it will just read until "...product ID :1234," then stop. After then, it start read new line from "Tag ID". As you guys can see the "|" is not close at end of ID 3. I wonder how to read string with line break. Please help.

Was it helpful?

Solution

Fix your csv. Values that include linebreaks or the delimeter-character, needs to be quoted. You should also remove line 2(the -----------------line) before importing.

Ex.

ID | Name | Message | User
1 | Joe | This is a text, error message | admin
2 | Mary | No error message, ID number: 9182734 | normal-user
3 | Terry | "This is a long text, long message. Product ID : 1234,
Tag ID : fhdj123, Please restart" | admin

Import:

PS > import-csv -Delimiter "|" .\Desktop\test.csv 

ID  Name   Message                                                    User       
--- -----  --------                                                   ----       
1   Joe    This is a text, error message                              admin      
2   Mary   No error message, ID number: 9182734                       normal-user
3   Terry  This is a long text, long message. Product ID : 1234,...   admin 

You could also replace the linebreaks in the file before importing, but I'm guessing that you need the values as-is.

OTHER TIPS

You will have to parse your import file and replace coma followed by line break sequence with just a coma. This could be ,`n or ,`r depending on the format of your file. This should do the trick:

gc example.txt | Out-String | % { 
    $_.replace(",`r",",").replace(",`n",",") } | Set-Content example.txt

and then run your import-csv

PS D:\>Import-Csv example.txt -Delimiter "|"

which will result in correct assignment of columns:

ID                      Name                    Message                User
---                     -----                   --------               ----
--------------------...
1                       Joe                     This is a text, err... admin
2                       Mary                    No error message, I... normal-user
3                       Terry                   This is a long text... admin

You could also remove the mulitple hyphens to make the file easier to read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top