Luboš Hubáček
Luboš Hubáček

Reputation: 25

AWK - parse second column from git status command

I need to parse second column from output above - problem is, that if filename contains spaces and is surrounded by quotation marks.

Command:

$ git status -u -s

Output:

 D README.md
 D mail/falover.sh
?? "ahh/Nov nnsdfd file.txt"
?? fremover.sh
?? mail/ahooooj
?? mail/awww
?? mail/file_test
?? mail/git.sh
?? mail/test

Thank you a lot!

Upvotes: 2

Views: 1127

Answers (6)

Ole Lukøje
Ole Lukøje

Reputation: 672

git status -u -s | awk -F '/' '{gsub("\"","");print $NF}'  

if I understood the problem correctly

Upvotes: 0

nbari
nbari

Reputation: 26955

Try using the -porcelain=2 option that will remove the double quotes so that later you could use something like:

$ git status --porcelain=2 | cut -d" " -f9-

Note that this will only return tracked files if want to get all the files including the untracked ones also within quotes to try this;

$ git status --porcelain | cut -c4-

With awk this could be used:

$ git status --porcelain | awk '{$1 = ""; print substr($0, 2)}'

It works by setting the first column to "" and then printing the full line $0 but removing the space used as a delimiter using substr. (This will force awk to rebuild the entire record $0 using spaces as the new delimiter - check @Ed Morton explanation in the comments & https://stackoverflow.com/a/15475578/1135424)

Check the Porcelain Format Version 2

Upvotes: 1

jthill
jthill

Reputation: 60393

Don't have git status calculate the status and then scrape the status off its output, instead use git ls-files -o or git ls-files -o --exclude-standard to simply list the files you want directly.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203985

Depending on if you want to retain the quotes or not:

$ sed -E 's/^ *[^ ]+ +//' file
README.md
mail/falover.sh
"ahh/Nov nnsdfd file.txt"
fremover.sh
mail/ahooooj
mail/awww
mail/file_test
mail/git.sh
mail/test

$ sed -E 's/^ *[^ ]+ +//; s/^"|"$//g' file
README.md
mail/falover.sh
ahh/Nov nnsdfd file.txt
fremover.sh
mail/ahooooj
mail/awww
mail/file_test
mail/git.sh
mail/test

If your sed doesn't support EREs by means of -E then:

$ sed 's/^ *[^ ]* *//; s/^"|"$//g' file
$ sed 's/^ *[^ ]* *//; s/^"//; s/"$//' file

Note that there is an edge case where this will fail and that is when your file name starts and ends with quotes (which is allowed for a UNIX file name). If that can happen then tell us about it in your question and include it in your sample input/output.

Upvotes: 1

Fabian N.
Fabian N.

Reputation: 3856

In case you don't actually need to use awk a simple cut would be enough:

git status -u -s | cut -d" " -f2-

-d" " sets space as the delimiter and -f2- selects every field except the first

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133610

In case you only need those lines which have " and you need 2nd field means values from " to " then following may help you.

your_command | awk 'match($0,/".*"/){print substr($0,RSTART+1,RLENGTH-2)}'

In case you want to print file names when " is present in output with all other line(which don't have " in them) then following may help you.

your_command | awk 'match($0,/".*"/){print substr($0,RSTART+1,RLENGTH-2);next} 1'

Upvotes: 1

Related Questions