I have a text file in which data is in below given format. Its a text delimited file with 10000 lines of data. Main columns are given below though there are many more columns.
Supplier_Name|Supplier_Address_1|Supplier_Postcode|VAT_Reg
ACCOMMODATION|P O Box 123|3404|11009341296
PROVIDERS|P O Box 543|6704|451895748
this is a file format … now through powershell script I want to see only those Supplier VAT_Reg id’s those are below 11 digit. Kindly guide me how to do this through script as i am new in this field.
That’s a delimited text file, which is very easy to read in PowerShell with the Import-Csv cmdlet. (Even though it’s not a comma-delimited file, there’s a parameter which lets you switch that). Try this:
@ Dave - I am trying to run your code but it is not giving me any output. I gave my file path where my file is placed but it is showing nothing on executing.
@Dave - Its working now… but it is showing results for all supplier’s but my requirement is to see only result for those supplier’s whose VAT_Reg id is below then 11 digit…
Please post your current command you are running. Dave’s command should do what you are asking, so we would need to look at the command you are running.
Ok, so let’s troubleshoot and see what is going on. When importing from a CSV, everything is typically a string, which contains Length property. If for some reason the value is being converted to a different type, (e.g. int) it could be causing your issue. Try running this and see what the output is:
Import-Csv -Path C:\Users\ankit\Downloads\SC\SupplierExtract.txt -Delimiter '|' | foreach{ "VAT_Reg {0} is {1} characters long and is type {2}" -f $_.VAT_Reg,$_.VAT_Reg.Length, $_VAT_Reg.GetType().Name)
Apologies for the late reply… I just stuck with this site login problem and it was late night too… really its a weird site… Anyhow i executed your given query but again its giving error
You cannot call a method on a null-valued expression.
At line:1 char:195
I tried to execute this query with both curly braces and parenthesis. In your code you have started with curly braces and end up with parenthesis so on running first time its giving error so I have run with both the parameters.
Ankit, Dave’s code should absolutely do the trick. I copied and pasted your data from the original question into a file called “input.txt”, then ran Dave’s online against it. It seemed to do the trick quite nicely.
Here is the Import-Csv before filtering out the ones longer than 11 characters
@Curtis@Dave - Your script is right. I am sorry… I am wrong…I also made one dummy text delimited file and insert only below values to it and it fetched the actual output from the file i.e. only for “PROVIDERS”
ACCOMMODATION P O Box 123 3404 11009341296
PROVIDERS P O Box 543 6704 451895748
But the issue is that is my real file have data of more than 10,000 of suppliers and it has in some different format… What I mean to say is that it has some blank delimited columns also. Might be because of that it is not picking up the right output over there. Could you please share with me your mail id’s so that I can share the data with you to sort out the issue.
@Curtis@Dave - My data is like in below given format in actual file - First row are columns name and then after that I have copied 3 rows of supplier data.
And here is the data set filters for VAT_Reg values less than 11 characters in length. Only 1 of the 3 records is returned, and it’s returned because because the VAT_Reg field for that record contains no value. In reality, your dataset has a bunch of fields in the records that have no value in them.
Yes @Smith you are right… that’s why I have copy pasted some of the data in this blog… hope you have seen it… Please have a look and kindly suggest how to do in this case?
Ankit, I don’t understand. Has the question changed? As I understand the current question it is how do you show all the records where the VAT_Reg value is less than 11 characters in length for your dataset. I believe we have have already shown how to do that. Put your data in a file and then use the command supplied to show only the records with less than 11 characters. I’m not sure what more you are trying to accomplish.
@Curtis - Sorry If i am wrong because i am newbie in PowerShell and not experts like you but the command you have given me that is showing that Suppliers name also in output which have VAT_Reg id’s with blank values. I don’t want to see the suppliers in output with blank VAT_Reg id’s. Is there any way out to remove those suppliers from the output?