views:

44

answers:

4

The end of git status looks like this:

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       Classes/Default.png
#       Classes/[email protected]
...

Since you might have any number of untracked files, I'm trying to tail from the end of the file to "Untracked files" and save it to a temp file, strip out the first three lines and convert the filenames to git add Classes/...

I can't seem to find a good way (other than maybe a different language) to tail up to a searchable expression. Thanks!

A: 

Pipe it to:

perl -e 'my ($o, $p) = (0, shift); while(<>){print if $o or $o = /$p/}' "$MY_REGEX"

Where $MY_REGEX is your pattern. In your case, probably '^\s{7}'.

amphetamachine
+2  A: 

Use sed to print everything from "Untracked files" to the end:

git status | sed -n '/Untracked files:$/,$p'

Then you just have to parse the filenames by removing the # character.

You can also use git status -s to get a shorter, more easily parsed output:

~$ git status -s
?? Classes/Default.png
?? Classes/[email protected]

This is a good application of awk, which lets you grep and extract at the same time:

~$ git status -s | awk '/\?\?/{print $2}'
Classes/Default.png
Classes/[email protected]

Alternatively: awk '{if ($1 == "??") print $2}'

You can also, of course, use git add to list (and add) untracked files.

jleedev
A: 

Use the tail command:

tail -$(($(wc -l < file.txt) - $(grep -n "Untracked files" file.txt|cut -d: -f1) - 2)) file.txt

How it works:
total number of lines in file = wc -l < file.txt
line number of "Untracked files" = grep -n "Untracked files" file.txt|cut -d: -f1
The -2 is to remove the top lines

Complete command with git add:

tail -$(($(wc -l < file.txt) - $(grep -n "Untracked files" file.txt|cut -d: -f1) - 2)) file.txt | tr -d '#'| while read line; do echo git add $line; done
dogbane
A: 

Solution using shell scripting.

First start reading the file in a while loop, keep a count of the number of lines read, break the loop when the required line is found. Using the count of lines tail the file and then extract file names using awk.

i=0; 
l=`wc -l filename | awk '{print $1}'`; 

while read line; 
do i=`echo $i + 1 | bc`; 
if [[ $line == "# Untracked files:" ]]; 
then break; 
fi; 
done < "filename"; 

tail -`echo $l -$i -2 | bc` filename | awk -F "/" '{print $NF}'

Here "filename" is the file you want to process

athena
you don't need to read the file yourself. Use `grep -n`. You don't need `bc` either. See my answer.
dogbane
@fahd: Yes. Your solution is simpler.
athena
Thanks so much for your help everyone! Got it working!
@user454999: Consider accepting an answer.
athena
Sorry Athena, it was my first time posting, I didn't even realize what you meant until I researched and for some reason I didn't see your request until now...