UNIX shell script question

Posted on 2014-08-07
Last Modified: 2014-08-07
I am writing the following command in the shell script:
var1=`/home/user1/Python/bin/bq --format prettyjson show mytab1 | grep "lastModifiedTime"`

The above returns:
  "lastModifiedTime": "1407393977076",

If I write:
var1=`/home/user1/Python/bin/bq --format prettyjson show mytab1 | grep "lastModifiedTime" | awk -F"\"" '{print $4}'`
The above returns 1407393977076

I only need the first 10 digits of the above number which is 1407393977
Could you please help me update the above command so that the above returns 1407393977 (not 1407393977076) ?
I need to preferably use a single command.
Thank you.
Question by:toooki
    LVL 27

    Accepted Solution

    try this

    var1=`/home/user1/Python/bin/bq --format prettyjson show mytab1 | grep "lastModifiedTime" | awk -F"\"" '{print $4}'`|cut -c 1-10

    Open in new window

    LVL 23

    Assisted Solution

    Since you're going to have pretty static output, you can skip the awk and go right to the cut:
    var1=`/home/user1/Python/bin/bq --format prettyjson show mytab1 | grep "lastModifiedTime" | cut -c 22-32`

    Open in new window

    LVL 11

    Expert Comment

    Well said, nemws1, except 22-32 is 11 chars, and we want 10.

    Should be this:
        var1=`/home/user1/Python/bin/bq --format prettyjson show mytab1 | grep "lastModifiedTime" | cut -c22-31`
    LVL 23

    Expert Comment

    Yup, off by one error on my part.

    Author Comment

    Worked perfectly.
    Thanks a lot.
    LVL 19

    Expert Comment

    grep then cut?  How about:

        var1=`/home/user1/Python/bin/bq --format prettyjson show mytab1 | awk '/lastModifiedTime/{print substr($0,21,10)}'

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. Please see for the updated article. It is avail…
    Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now