How do I pipe output to date -d "value"?
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.
Related videos on Youtube
user16716646
Updated on September 18, 2022Comments
-
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 over 10 yearsThank you, this solved the problem! Also appreciate you explaining the reason behind why it wasn't working.
-
Petrus K. about 7 yearsThis should be the accepted answer.
-
Manu over 6 yearsIs 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 over 6 years@Manu not per se, dateutils have
dategrep
for that specific use case. -
Ken Sharp about 6 yearsThis is so much easier! If only I could read I might have found this in he man page.
-
Eran Ben-Natan almost 3 yearsI have used the -f option, but your answer with the pipeline format is very elegant!
-
user16716646 about 2 yearsBut 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 about 2 yearsArh 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 about 2 years
map
does not change the array, it just returns a new array. See using map js -
Mehdi Rahimi about 2 yearsNo, you are returning the
addressArr
inside the map. and please fix that and putreturn
outside of the map and instead of map useforEach
method. but I will update my post to give you what I'm saying. -
user16716646 about 2 yearsOf course!! Thank you very much.
-
user16716646 about 2 yearsThank you very much, got it now!
-
Shivam Jha about 2 yearsHappy to help. If this answer helped you, you can accept it by clicking
✅
button on it. @user16716646