How to use awk output in another command?

Therefore, I need to convert the date to a different format. With the bash pipeline, I take the date from the last login to the console and pull the corresponding bits with awk, for example:

last $USER | grep console | head -1 | awk '{print $4, $5}' 

Which outputs: Aug 08 ($ 4 = Aug $ 5 = 08, in this case.)

Now I want to take "August 08" and put it in the date command to change the format to a numeric date.

Which would look something like this:

 date -j -f %b\ %d Aug\ 08 +%m-%d 

Outputs: 08-08

I have a question: how to add this to my pipeline and use the awk variables $ 4 and $ 5, where is β€œAugust 08” in this date command?

+5
source share
4 answers

You just need to use command substitution:

 date ... $(last $USER | ... | awk '...') ... 

Bash will evaluate the command / pipeline inside $(...) and place the result there.

+5
source

Get awk to call date :

 ... | awk '{system("date -j -f %b\ %d \"" $4 $5 "\" +%b-%d")}' 

Or use process substitution to get awk output:

 date -j -f %b\ %d "$(... | awk '{print $4, $5}')" +%b-%d 
+3
source

I assume you already tried this?

 last $USER | grep console | head -1 | awk | date -j -f %b\ %d $4 $5 +%b-%d 
0
source

Using reverse ticks should work to get your long pipeline output to date.

  date -j -f% b \% d \ `last $ USER |  grep console |  head -1 |  awk '{print $ 4, $ 5}' \ `+% b-% d
0
source

Source: https://habr.com/ru/post/959102/


All Articles