1

I do git status --short at the root (of the Repository), and it lists the file path relative to the root like as follows:

M NumericalProgramming1Src/FloatingPointNumber.md
M NumericalProgramming1Src/NumericalProgramming.md

I want to prefix all the path with a value stored in a variable: CustomPrefix=My/Path/To/Root/ as follows

M My/Path/To/Root/NumericalProgramming1Src/FloatingPointNumber.md
M My/Path/To/Root/NumericalProgramming1Src/NumericalProgramming.md

How could I achieve this?

NOTE: Sometimes git status has more than one character in the beginning.

Porcupine
  • 5,885
  • 2
  • 19
  • 28

1 Answers1

1

With awk you simply call:

custom="/test/"
git status --short | awk -v cp="$custom" '{$2=cp$2}1'

For example, while git status --short yields

M org/languagetool/resource/de/added.txt

The above command yields:

M /test/org/languagetool/resource/de/added.txt
F. Knorr
  • 3,045
  • 15
  • 22
  • How can we use bash variable `CustomPrefix` inside `awk` command instead of `/custom/prefix/`? I am asking because the string stored in this variable changes value in a `for` loop. – Porcupine Nov 12 '18 at 20:07
  • 1
    I have modified my answer accordingly; First, I set a new shell variable `custom` which I use inside my awk command via `-v cp="$custom"` – F. Knorr Nov 12 '18 at 20:12