1

I have the following test.txt. Using below command its printing output: xvf-9c3683ff. However I need the output xvf-bcb500df. It is based on its last date.

cat test.txt | sort -k2 | awk '{print $2}' | sed 's/"//g' | grep xvf | head -1

test.txt

{
    "date": "2017-01-30T10:55:46.000Z", 
    "Id": "xvf-9c3683ff"
}, 
{
    "date": "2017-01-26T12:58:20.000Z", 
    "Id": "xvf-bcb500df"
}, 
{
    "date": "2017-01-31T18:33:20.000Z", 
    "Id": "xvf-ee07b28d"
}

output should print below result.
xvf-bcb500df

3

Assuming this is a JSON formatted file (it lacks [ in the beginning and ] at the end), with jq:

$ jq 'sort_by(.date)' data.json
[
  {
    "date": "2017-01-26T12:58:20.000Z",
    "Id": "xvf-bcb500df"
  },
  {
    "date": "2017-01-30T10:55:46.000Z",
    "Id": "xvf-9c3683ff"
  },
  {
    "date": "2017-01-31T18:33:20.000Z",
    "Id": "xvf-ee07b28d"
  }
]

To get the first Id:

$ jq 'sort_by(.date)|.[0].Id' data.json
"xvf-bcb500df"

To get it without the quotes:

$ jq -r 'sort_by(.date)|.[0].Id' data.json
xvf-bcb500df

Data in a structured and standard format should be parsed with a parser written for the task. There are readily available parsers for use in shell script or on the command line for several common formats, including JSON, XML, CSV and others.

0

Step 1: reajusting format:

$ cat data.json | json -ga date Id
2017-01-30T10:55:46.000Z xvf-9c3683ff
2017-01-26T12:58:20.000Z xvf-bcb500df
2017-01-31T18:33:20.000Z xvf-ee07b28d

Next step: sort and print just ID:

cat data.json | json -ga date Id | sort | awk 'NR==1{print $2}'

json comand line tool is documented in http://trentm.com/json/ . If necessary,

  • install node
  • and sudo npm install -g json
0

You can try the following:

| awk '{print $2}' | sed -n '/"/N;s/\n//;s/"//gp' | sort | awk -F, '/xvf/{print $2}' | head -n1

Example:

$ echo '{
    "date": "2017-01-30T10:55:46.000Z", 
    "Id": "xvf-9c3683ff"
}, 
{
    "date": "2017-01-26T12:58:20.000Z", 
    "Id": "xvf-bcb500df"
}, 
{
    "date": "2017-01-31T18:33:20.000Z", 
    "Id": "xvf-ee07b28d"
}' | awk '{print $2}' | sed -n '/"/N;s/\n//;s/"//gp' | sort | awk -F, '/xvf/{print $2}' | head -n1
xvf-bcb500df

EDIT - Python approach

Make sure your data is valid JSON, then you can use the following script:

import json

with open('test.txt', 'rt') as file:
   data = sorted(json.load(file), key=lambda x: x['date'])
   print(data[0]['Id'])
  • This works !! Just modify the command to remove the double quote. | awk '{print $2}' | sed -n '/"/N;s/\n//p' | sort | awk -F, '{print $2}' | sed 's/"//g' | head -n1 – user213618 Feb 1 '17 at 19:05
  • @user213618 Please see edited answer. – NarūnasK Feb 2 '17 at 10:47
  • edited command works !! but it does not work with the following case. { "date": "2017-01-30T10:55:46.000Z", "Id": "xvf-9c3683ff", "mr": "available" }, { "date": "2017-01-26T12:58:20.000Z", "Id": "xvf-bcb500df" "mr": "active" }, { "date": "2017-01-31T18:33:20.000Z", "Id": "xvf-ee07b28d" "mr": "pending" } – user213618 Feb 14 '17 at 7:15
  • You must use properly formatted JSON, otherwise it will not work, thus it must be: echo -e '{\n"date": "2017-01-30T10:55:46.000Z",\n"Id": "xvf-9c3683ff", "mr": "available"\n},\n{\n"date": "2017-01-26T12:58:20.000Z",\n"Id": "xvf-bcb500df" "mr": "active"\n},\n{\n"date": "2017-01-31T18:33:20.000Z",\n"Id": "xvf-ee07b28d" "mr": "pending"\n}' | awk '{print $2}' | sed -n '/"/N;s/\n//;s/"//gp' | sort | awk -F, '/xvf/{print $2}' | head -n1. If your real intent is JSON parsing - you should really look to other answers, I only showed you how to do it using standard tools set. – NarūnasK Feb 14 '17 at 10:27
  • @user213618 See edited question, to learn how to use Python to parse json. You just need to make sure, your original sample is enclosed in square brackets [, ]. – NarūnasK Feb 14 '17 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.