Issue
Team, I have specific lines to be printed.
output is below.
what i want is:
1 - whenever there is Rx where x=any number and R is alphabet R exists in first column of below output it should print me the $3 that is 3rd column.
2 - whenever there is no Rx in first column but M it should print column 2. so expected output is below
Command> git diff HEAD~1 --name-status
R063 src/services/jamoperator/events/BUILD.bazel src/common/jam/operator/events/BUILD.bazel
R060 src/services/jamoperator/events/events.go src/common/jam/operator/events/events.go
R100 src/services/jamoperator/events/events_test.go src/common/jam/operator/events/events_test.go
M src/services/jamoperator/operator_test.go
R100 src/services/secrets/keys/keys.go src/services/secrets/keys/keys.go1
M src/services/secrets/main.go
expected output.
src/common/jam/operator/events/BUILD.bazel
src/common/jam/operator/events/events.go
src/common/jam/operator/events/events_test.go
src/services/jamoperator/operator_test.go
src/services/secrets/keys/keys.go1
src/services/secrets/main.go
i tried and got 1st part but want to assure is this the right way?
git diff HEAD~1 --name-status | awk '$1 ~ /R./ { print $3}'
also the second part like this
git diff HEAD~1 --name-status | awk '$1 ~ /M/ { print $2}'
so are both my approaches correct? is there a way I can build single expression?
Solution
Combining your two awk
all in one:
git diff HEAD~1 --name-status | awk '$1 ~ /^R[0-9]/{print $3} $1 ~ /^M/{print $2}'
Decomposed:
git diff HEAD~1 --name-status |
awk '
$1 ~ /^R[0-9]/ {print $3}
$1 ~ /^M/ {print $2}
'
Output
src/common/jam/operator/events/BUILD.bazel
src/common/jam/operator/events/events.go
src/common/jam/operator/events/events_test.go
src/services/jamoperator/operator_test.go
src/services/secrets/keys/keys.go1
src/services/secrets/main.go
Answered By - Gilles Quénot Answer Checked By - Robin (WPSolving Admin)