0

I am trying to write shell script to find Approved user in Jenkins Pipeline log file. I put here a part of pipeline script. how to pick 'MPPortal Administrator' or any user name following by 'Approved by' words? I am new to shell scripting and I have no idea to do it.

Timeout set to expire in 1 hr 30 min
[Pipeline] {
[Pipeline] input
APPROVE?
Proceed or Abort
Approved by MPPortal Administrator
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Library Policy Evaluation)
[Pipeline] 

What I tried

grep Approved pipeline.log

then output was

Approved by MPPortal Administrator

expected Output:

MPPortal Administrator
HatLess
  • 10,622
  • 5
  • 14
  • 32
Zero5
  • 151
  • 7

3 Answers3

1

Using sed

$ sed -n 's/^Approved[^[:upper:]]*\(.*\)/\1/p' input_file
MPPortal Administrator

Using awk

$ awk '$1=="Approved" {print $3,$4}' input_file
MPPortal Administrator

Using grep and cut

$ grep  '^Approved by' input_file | cut -d' ' -f3-4
MPPortal Administrator
HatLess
  • 10,622
  • 5
  • 14
  • 32
0

There are vaious ways of doing this as the current answers already show. Here are a couple more:

awk and sed: search line, delete part using sub:

$ awk '/Approved by/{sub(/Approved by */,""); print}' file
$ sed '/Approved by/!d;s/Approved by *//'
$ sed -n '/Approved by/s/Approved by *//'

awk: search line, delete part by redefining fields:

$ awk '/Approved by/{$1=$2="";$0=$0;$1=$1;print}' file

This searches for the words "Approved by". If those words appear in the line, then delete the first and second field (these are the words "Approved by"). This will keep a couple of spaces before the remainder of the string. So, by reassigning the main record to itself ($0=$0), we redefine the fields. This still has the space here, but now we can reassign $1=$1 which rewrites the record and removes the space. Hacky, but works.

grep using perl regular expressions:

$ grep -oP "Approved by\s\K.*" file
$ grep -oP "(?<=Approved by).*" file

In the first part we search for the full line, but using \K we delete everything from the match that comes before this (\s is a blank) The second one uses a look-behind statement.

kvantour
  • 25,269
  • 4
  • 47
  • 72
0

Two options to consider:

  1. Use 'awk'
  2. Use grep to locate entry, sed to filter unwanted prefix.

Using awk:

awk '$1 == "Approved" { print $3, $4 }' pipeline.log

Using grep/sed

grep 'Approved' pipeline.log | sed -e 's/^Approved by //'
dash-o
  • 13,723
  • 1
  • 10
  • 37