添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I have to extract columns from a text file explained in this post:

Extracting columns from text file using Perl one-liner: similar to Unix cut

but I have to do this also in a Windows Server 2008 which does not have Perl installed. How could I do this using PowerShell? Any ideas or resources? I'm PowerShell noob...

And if you want the data in those columns printed on the same line:

Get-Content test.txt | Foreach {"$(($_ -split '\s+',4)[0..2])"}

Note that this requires PowerShell 2.0 for the -split operator. Also, the ,4 tells the the split operator the maximum number of split strings you want but keep in mind the last string will always contain all extras concat'd.

For fixed width columns, here's one approach for column width equal to 7 ($w=7):

$res = Get-Content test.txt | Foreach {
           $i=0;$w=7;$c=0; `
           while($i+$w -lt $_.length -and $c++ -lt 2) {
               $_.Substring($i,$w);$i=$i+$w-1}}

$res will contain each column for all rows. To set the max columns change $c++ -lt 2 from 2 to something else. There is probably a more elegant solution but don't have time right now to ponder it. :-)

Thanks, but this doesn't seem to work. I'm running PowerShell 2 and try to extract first two columns from my fixed-width .dat file (text file) – atricapilla Mar 24 '10 at 6:01 The cut example you link to uses a space delimiter and grabs columns 1 thru 3. If this doesn't apply to your case, can you state what your requirements are? Sounds like fixed column width instead of delimited. If so, what is the column width? – Keith Hill Mar 24 '10 at 17:24 My data is in fixed-width text file (spaces between). I modified your code and got this: Get-Content text.txt | Foreach {"$($_.split()[0..2])"}. This gets me quite near, but this generates addition row breks between rows. – atricapilla Mar 25 '10 at 6:12 Make sure $OFS is set to either $null or something like ' '. Also did you try $_ -split '\s+',3? That should get rid of the extra empty entries. The way string.split works is that each consecutive space after the first will result in an extra empty string resturned. – Keith Hill Mar 25 '10 at 14:13 I tried this like C:> .\Extract_Two_Columns_From_Text_File.ps1 > twocols.dat But it did not print anything? – atricapilla Mar 24 '10 at 6:08 If you have mulitple spaces between columns (quite common) this will produce a bunch of empty entries. This is why Jared uses the [StringSplitOptions]::RemoveEmptyEntries enum value. – Keith Hill Mar 24 '10 at 19:17 Yes, this produces the same: Get-Content text.txt | Foreach {"$($_.split()[0..2])"}. – atricapilla Mar 25 '10 at 6:15 I tried also this: Get-Content text.txt | Foreach {"$($_.split(" ", [StringSplitOptions]::RemoveEmptyEntries))[0..2])"}, but it still produces those empty lines. – atricapilla Mar 25 '10 at 6:28

Try this. This will help to skip initial rows if you want, extract/iterate through columns, edit the column data and rebuild the record:

$header3 = @("Field_1","Field_2","Field_3","Field_4","Field_5")     
Import-Csv $fileName -Header $header3 -Delimiter "`t" | select -skip 3 | Foreach-Object {
    $record = $indexName 
    foreach ($property in $_.PSObject.Properties){
        #doSomething $property.Name, $property.Value
            if($property.Name -like '*CUSIP*'){
                $record = $record + "," + '"' + $property.Value + '"' 
            else{
                $record = $record + "," + $property.Value 
        $array.add($record) | out-null  
        #write-host $record                         
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.2.12.36053