![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | How do I read a text file and sort text by fixed positions? I need to read the content of a file and sort the content by fixed width. Example file: sample_data.txt 23abdc1133cdde 14zzwd0034kkhe 65ppok7780hyyh Read in the content and sort text by fixed position (startpos:1,len:2)(startpos:3,len:4)(startpos:6,len:4) I'm having problems sorting the text after i read the file. I think i need to use regex and sort the matches object. This is what i have so far: Get-Content c:\text_acct_level.txt | ForEach-Object { if ($_ -match [regex]"^.{2}(?<fld1>(.{9}))(?<fld2>(.{4}))(?<fld3>(.{20})).*"){$matches}}|sort-Object $matches[1],$matches[2],$matches[3] Its not working but at maybe it will help you understand where I'm getting lost. Thanks, Cornelius |
My System Specs![]() |
| | #2 (permalink) |
| | Re: How do I read a text file and sort text by fixed positions? "Cornelius" <Cornelius@discussions.microsoft.com> wrote in message news:1544EB07-3A08-47EE-BB53-0DF3E2C12074@microsoft.com... >I need to read the content of a file and sort the content by fixed width. > Example file: sample_data.txt > 23abdc1133cdde > 14zzwd0034kkhe > 65ppok7780hyyh > > Read in the content and sort text by fixed position > (startpos:1,len:2)(startpos:3,len:4)(startpos:6,len:4) I am lost with your question: if you want to sort the file by the first two characters then the next four characters and finally the last four characters, won't sorting the whole line by default produce the expected result? PS> get-content sample_data.txt | sort However, if you want to sort on non-sequential patterns of your text the following should work:- --- sample_data.txt --- 23abdc1133cdde 14zzwd0034kkhe 65ppok7780hyyh 12abdc1133cdde --- sample_data.txt --- PS> # sort on 3rd to 6th chars, then on 1st to 2nd chars: PS> get-content sample_data.txt | sort {$_[2..5]},{$_[0..1]} 12abdc1133cdde 23abdc1133cdde 65ppok7780hyyh 14zzwd0034kkhe Hope that helps, Jacques |
My System Specs![]() |
| | #3 (permalink) |
| | Re: How do I read a text file and sort text by fixed positions? Sorry I know the post was a mess and not very clear. I'm trying to sort non-sequential It worked great! Thank you. Powershell is so much fun when you know what you are doing. It looks like i need more practice... ![]() "Jacques Barathon [MS]" wrote: > "Cornelius" <Cornelius@discussions.microsoft.com> wrote in message > news:1544EB07-3A08-47EE-BB53-0DF3E2C12074@microsoft.com... > >I need to read the content of a file and sort the content by fixed width. > > Example file: sample_data.txt > > 23abdc1133cdde > > 14zzwd0034kkhe > > 65ppok7780hyyh > > > > Read in the content and sort text by fixed position > > (startpos:1,len:2)(startpos:3,len:4)(startpos:6,len:4) > > I am lost with your question: if you want to sort the file by the first two > characters then the next four characters and finally the last four > characters, won't sorting the whole line by default produce the expected > result? > > PS> get-content sample_data.txt | sort > > However, if you want to sort on non-sequential patterns of your text the > following should work:- > > --- sample_data.txt --- > 23abdc1133cdde > 14zzwd0034kkhe > 65ppok7780hyyh > 12abdc1133cdde > --- sample_data.txt --- > > PS> # sort on 3rd to 6th chars, then on 1st to 2nd chars: > PS> get-content sample_data.txt | sort {$_[2..5]},{$_[0..1]} > 12abdc1133cdde > 23abdc1133cdde > 65ppok7780hyyh > 14zzwd0034kkhe > > Hope that helps, > Jacques > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How do I read a text file and sort text by fixed positions? You can also use ADO the read fixed length delimited files, by using a schema.ini (can be generated in controlpanel) I describe this method in this entry on my old blog : http://mow001.blogspot.com/2006/07/w...delimited.html Greetings /\/\o\/\/ http://thePowerShellGuy.com "Cornelius" <Cornelius@discussions.microsoft.com> wrote in message news:CC9895DF-ED1B-4EAF-8421-CB78157DA9F7@microsoft.com... > Sorry I know the post was a mess and not very clear. I'm trying to sort > non-sequential > > It worked great! Thank you. > Powershell is so much fun when you know what you are doing. It looks like > i > need more practice... ![]() > > "Jacques Barathon [MS]" wrote: > >> "Cornelius" <Cornelius@discussions.microsoft.com> wrote in message >> news:1544EB07-3A08-47EE-BB53-0DF3E2C12074@microsoft.com... >> >I need to read the content of a file and sort the content by fixed >> >width. >> > Example file: sample_data.txt >> > 23abdc1133cdde >> > 14zzwd0034kkhe >> > 65ppok7780hyyh >> > >> > Read in the content and sort text by fixed position >> > (startpos:1,len:2)(startpos:3,len:4)(startpos:6,len:4) >> >> I am lost with your question: if you want to sort the file by the first >> two >> characters then the next four characters and finally the last four >> characters, won't sorting the whole line by default produce the expected >> result? >> >> PS> get-content sample_data.txt | sort >> >> However, if you want to sort on non-sequential patterns of your text the >> following should work:- >> >> --- sample_data.txt --- >> 23abdc1133cdde >> 14zzwd0034kkhe >> 65ppok7780hyyh >> 12abdc1133cdde >> --- sample_data.txt --- >> >> PS> # sort on 3rd to 6th chars, then on 1st to 2nd chars: >> PS> get-content sample_data.txt | sort {$_[2..5]},{$_[0..1]} >> 12abdc1133cdde >> 23abdc1133cdde >> 65ppok7780hyyh >> 14zzwd0034kkhe >> >> Hope that helps, >> Jacques >> >> |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How do I read a text file and sort text by fixed positions? You can specify a scriptblock as an argument to the sort command that will be used to calculate the value to sort on. For example: PS (23) > $text.split("`n") | sort # default sort 14zzwd0034kkhe 23abdc1133cdde 65ppok7780hyyh PS (24) > $text.split("`n") | sort {[int] ($_.substring(1,1) + $_.substring(6,4)) } # numeric sort on 2nd char + middle num string 23abdc1133cdde 14zzwd0034kkhe 65ppok7780hyyh PS (25) > -bruce -- Bruce Payette [MSFT] Windows PowerShell Technical Lead Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Cornelius" <Cornelius@discussions.microsoft.com> wrote in message news:1544EB07-3A08-47EE-BB53-0DF3E2C12074@microsoft.com... >I need to read the content of a file and sort the content by fixed width. > Example file: sample_data.txt > 23abdc1133cdde > 14zzwd0034kkhe > 65ppok7780hyyh > > Read in the content and sort text by fixed position > (startpos:1,len:2)(startpos:3,len:4)(startpos:6,len:4) > > I'm having problems sorting the text after i read the file. I think i need > to use regex and sort the matches object. > > This is what i have so far: > > Get-Content c:\text_acct_level.txt | ForEach-Object { if ($_ -match > [regex]"^.{2}(?<fld1>(.{9}))(?<fld2>(.{4}))(?<fld3>(.{20})).*"){$matches}}|sort-Object > $matches[1],$matches[2],$matches[3] > > Its not working but at maybe it will help you understand where I'm getting > lost. > > Thanks, > Cornelius > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: How do I read a text file and sort text by fixed positions? Bruce its cool to see your reply, being that I'm on Chapter 5 of your book. I guess I should have figured it out but, for some reason I just didn't think about the get-content sending an array object. I'm learning so much so fast, I have to use it daily to retain most of what I'm learning. Thanks for writing a great book. Sincerely, Cornelius "Bruce Payette [MSFT]" wrote: > You can specify a scriptblock as an argument to the sort command that will > be used to calculate the value to sort on. For example: > > PS (23) > $text.split("`n") | sort # default sort > 14zzwd0034kkhe > 23abdc1133cdde > 65ppok7780hyyh > PS (24) > $text.split("`n") | sort {[int] ($_.substring(1,1) + > $_.substring(6,4)) } # numeric sort on 2nd char + middle num string > 23abdc1133cdde > 14zzwd0034kkhe > 65ppok7780hyyh > PS (25) > > > -bruce > > -- > Bruce Payette [MSFT] > Windows PowerShell Technical Lead > Microsoft Corporation > This posting is provided "AS IS" with no warranties, and confers no rights. > > > > "Cornelius" <Cornelius@discussions.microsoft.com> wrote in message > news:1544EB07-3A08-47EE-BB53-0DF3E2C12074@microsoft.com... > >I need to read the content of a file and sort the content by fixed width. > > Example file: sample_data.txt > > 23abdc1133cdde > > 14zzwd0034kkhe > > 65ppok7780hyyh > > > > Read in the content and sort text by fixed position > > (startpos:1,len:2)(startpos:3,len:4)(startpos:6,len:4) > > > > I'm having problems sorting the text after i read the file. I think i need > > to use regex and sort the matches object. > > > > This is what i have so far: > > > > Get-Content c:\text_acct_level.txt | ForEach-Object { if ($_ -match > > [regex]"^.{2}(?<fld1>(.{9}))(?<fld2>(.{4}))(?<fld3>(.{20})).*"){$matches}}|sort-Object > > $matches[1],$matches[2],$matches[3] > > > > Its not working but at maybe it will help you understand where I'm getting > > lost. > > > > Thanks, > > Cornelius > > > > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Sort text file | PowerShell | |||
| Simple sort text file | PowerShell | |||
| How to read text file | VB Script | |||
| How to read and write a text file? | VB Script | |||
| Using ADO to read a text file | PowerShell | |||