How do I pipe output to date -d "value"?

1,595

Solution 1

gmt="$(grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[')"
date -d "$gmt"

Or, if you prefer the pipeline format:

grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[' | { read gmt ; date -d "$gmt" ; }

The problem is that date does not use stdin. Thus, we have to capture the stdin into a variable (called gmt here) and then supply that on the command line to date.

Sample output from the second approach:

$ echo  "2014-01-30 05:04:27 GMT" | { read gmt ; date -d "$gmt" ; }
Wed Jan 29 21:04:27 PST 2014

Solution 2

If you're using GNU date from a sufficiently recent coreutils, there's date -f, from the help screen:

-f, --file=DATEFILE       like --date once for each line of DATEFILE

So your attempt 4 could have been:

$ grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[' | date -f -

the last - stands for stdin.

Share:
1,595

Related videos on Youtube

user16716646
Author by

user16716646

Updated on September 18, 2022

Comments

  • user16716646
    user16716646 over 1 year

    I'm trying to test a function with jest, and I simply can figure out what's wrong? It keeps saying it expects to return the output, but got undefined. I have tested the function elsewhere where it seems to return the correct array.

    I'm calling my my function and passing it an Object, it's then supposed to return an array. Then I'm calling .toEqual(output) which is an array.

    //This is my function
    const allAddresses = [
    ];
    
    const updateAllAddresses = (obj) => {
      const transferAmount = obj.transferAmount;
      const to = obj.to;
      const transferAddress = obj.address;
      const newBalance = obj.newBalance;
      const addressArr = [...allAddresses];
      console.log("This addressArr", addressArr);
      console.log("this is obj", obj);
      //To set your account to the new balance after transfer and
      //to check if the address you transfer to is your own account
      addressArr.map((address) => {
        if (address.account === transferAddress) {
          console.log("This is inside the map !!!!");
          address.balance = Number(newBalance);
        }
        if (address.account === to) {
          console.log("2");
          address.balance = Number(transferAmount) + Number(address.balance);
        }
        console.log("last part of the testing", addressArr);
        return addressArr;
      });
    };
    
    const obj = {
    };
    const output = [
    ];
    
    
    //This is my test
    describe("Update array", () => {
      test("update the array with the new information", () => {
        expect(updateAllAddresses(obj)).toEqual(output);
      });
    });
    
  • LeigerGaming
    LeigerGaming over 10 years
    Thank you, this solved the problem! Also appreciate you explaining the reason behind why it wasn't working.
  • Petrus K.
    Petrus K. about 7 years
    This should be the accepted answer.
  • Manu
    Manu over 6 years
    Is it possible to compare the output of the above command to a particular date. Eg. I need to list all dates older than "Wed Jan 30 21:04:27 PST 2014"
  • hroptatyr
    hroptatyr over 6 years
    @Manu not per se, dateutils have dategrep for that specific use case.
  • Ken Sharp
    Ken Sharp about 6 years
    This is so much easier! If only I could read I might have found this in he man page.
  • Eran Ben-Natan
    Eran Ben-Natan almost 3 years
    I have used the -f option, but your answer with the pipeline format is very elegant!
  • user16716646
    user16716646 about 2 years
    But I do return the adressArr on the last line of the function, after I have edited the Array. If i add a return before the map, it add three times undefined and doesn't pass the test.
  • user16716646
    user16716646 about 2 years
    Arh I see, about the return inside the map! I'm sorry but what do you mean regarding the variable will be removed on next iteration? I have used the spread operator to save the array of objects in the variable.
  • Shivam Jha
    Shivam Jha about 2 years
    map does not change the array, it just returns a new array. See using map js
  • Mehdi Rahimi
    Mehdi Rahimi about 2 years
    No, you are returning the addressArr inside the map. and please fix that and put return outside of the map and instead of map use forEach method. but I will update my post to give you what I'm saying.
  • user16716646
    user16716646 about 2 years
    Of course!! Thank you very much.
  • user16716646
    user16716646 about 2 years
    Thank you very much, got it now!
  • Shivam Jha
    Shivam Jha about 2 years
    Happy to help. If this answer helped you, you can accept it by clicking button on it. @user16716646