1

Is there a way in PowerShell to sort data that does not have a header. I cannot use | sort-object dateViewed because my data does not contain a header. I could use a script to create a new object with a header, but is there a way to sort by the 3rd column?

If I have a cli API aws get-vols that returns data like:

disk00   123456  20180103

disk00   222222  20180101

disk00   333333  20180102

I'm looking for the easy way out.

aws get-vols | sort-object (specify something that means column 3)

And i would see:

disk00   222222  20180101

disk00   333333  20180102

disk00   123456  20180103
1
  • what kind of data are you getting from aws get-vols ? Commented Jan 4, 2018 at 22:17

2 Answers 2

1

You might be better off using the AWS PowerShell module instead, which probably returns a real object.

But to answer your question, I will assume the return from aws get-vols is a bunch of separate lines, which are really just strings, where each "column" is separated by a TAB.

In that case, treat it like a CSV with a special delimiter:

aws get-vols |
    ConvertFrom-Csv -Delimiter "`t" -Header Disk,Id,DateViewed |
    Sort-Object -Property DateViewed

You can use the same approach if the separator is multiple spaces instead of tab, it's just a little more annoying:

aws get-vols |
    ForEach-Object -Process {
        # replace all contiguous whitespace with a comma
        # hope you don't have column values with spaces ;)

        $_ -replace '\s+', ','  
    } |
    ConvertFrom-Csv -Header Disk,Id,DateViewed |
    Sort-Object -Property DateViewed
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer worked! The data does have tabs, so your first solution worked as I needed. I am using the AWS powershell module. In my post, I edited out the AWS details to keep the issue related to powershell. I'm actually calling 'aws ec2 describe-snapshots --filters "Name=volume-id, Values=vol-xxxxxxxxxxx". I also tried Bill_Stewart's solution but ran into a problem (if interested, see below). it is very cool that with the -header, I can set the column names. Thanks again for the quick help.
@linux_driver Glad to hear it! But that command doesn't look like the PowerShell module. PowerShell uses Verb-Noun syntax, and takes its parameters with a single - and spaces between parameter name and value (not =). Also, they return actual objects not lines of text. You seem to be using the AWS CLI. I highly recommend going to the PowerShell module if you're going to be using PowerShell to deal with the values, your life will be much easier.
1

If you have PowerShell 5.0 or newer, you should be able to say something like this with the ConvertFrom-String cmdlet:

aws get-vols | ConvertFrom-String | Sort-Object P3

But I agree with briantist that if there are cmdlets you should use those instead of parsing text.

4 Comments

This is a good suggestion; ConvertFrom-String can do some pretty nice things by just throwing text at it. The only thing I'd caution is that it isn't yet included in PowerShell Core. It's currently slated for 6.1.0 (but 6.0.0 isn't even out yet, so no idea when or if it will be available again in future versions of PowerShell).
I tried this but PS threw an error (ConvertFrom-String : The term 'ConvertFrom-String' is not recognized as the name of a cmdlet...). Running powershell v 4 on Server 2012 R2. it's cool though because solution from briantist worked fine.
@linux_driver Bill got the version mixed up. It was introduced in PowerShell v5.
Correct, it was added in version 5. Sorry for the typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.