2017-11-27 20:11:13,632 INFO  abc: [java: Thread-389579]: Completed executing command
2017-11-27 20:11:13,632 INFO  abc: [java: Thread-389579]: OK
2017-11-27 20:11:13,632 INFO  abc: [java: Thread-389579]: <completed>
2017-11-27 20:11:13,632 INFO  abc: [java: Thread-389579]: release for adeo/student

My approaches:

awk 'today="$(date +'%Y-%m-%d')" ($1 = today) && /Completed executing command/ {cnr = NR} NR == (cnr+3) {gsub(/\/.*/,"",$NF); print $NF}' filename
awk 'today="$(date +'%Y-%m-%d')" ($1 == today) && /Completed executing command/ { cnr = NR } NR == (cnr+3) { sub("/.*","",$NF); print $NF }' filename

We want to print only adeo and student in two different variables. Condition is if $1 is equal to current date then it should search for Completed executing command in the entire file and then skip two lines and then print adeo and student. (The name adeo/student is not fixed it changes, so in above case we want adeo and student but if there is some other name, that should appear instead).

From our approach we are getting only adeo. How can we achieve this using awk/sed/grep or any other tool?

  • Its an duplicate, But i am not getting how to get that. Beginner at awk – user682606 Nov 28 '17 at 1:31
  • Remove sub("/.*","",$NF); – John1024 Nov 28 '17 at 1:47
  • It will print adeo/student. Not like name1=adeo name2=student currently what i did is i stored awk result in one variable and applied cut and it worked – user682606 Nov 28 '17 at 1:59
  • 4
    duplicate of stackoverflow.com/q/47488077/7552 and unix.stackexchange.com/q/407303/4667. Don't ask the same question on multple sites. – glenn jackman Nov 28 '17 at 2:18
  • 1
    @glennjackman I'm voting to reopen this, and I urge you to do so as well. At least if the links are correct, both those other questions have been deleted. Even if they hadn't been, this has valuable answers, which have been upvoted. It just makes no sense for this to be closed. – Eliah Kagan Nov 29 '17 at 22:13

I gather from the comments that you want the output to look like name1=adeo name2=student. If that is the case, try:

$ awk -v today="$(date +'%Y-%m-%d')"  '$1 == today && /Completed executing command/ { cnr = NR } cnr && NR == cnr+3 {sub(/\//, " name2=", $NF); print "name1="$NF }' filename
name1=adeo name2=student

One way you can approach using external variables is via placing today into environment,and use ENVIRON["today"] within awk. Using gsub() is also unnecessary, if you want to just get the name and student part and if they are always going to be last item in the line.

$ today=$(date +'%Y-%m-%d') awk '$1 = ENVIRON["today"] && /Completed executing command/ {cnr = NR} NR == (cnr+3) {print $NF}' input.txt                                

Your Answer

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