Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Im pretty stupid when in comes to bash and I was hoping someone could help me.

I would like to take the output of

hg resolve -l

which is in the format

R somefile/filename
U somefile/filename
R somefile/filename
U somefile/filename

and be able to use the first line that starts with a 'U' as an argument, so i could do things like

open -t <first U file>

and

hg resolve -m <first U file>

it would be great it someone could give me some pointers about the best way to do this!

Thanks

EDIT: solution from answers below, a simple script. Thanks guys!

#!/bin/bash

# Script that gets the first file from hg resolve | grep U and passes to default text editor

first_u_file=$(hg resolve -l | grep -m1 '^U')
first_u_file=${first_u_file#U }
echo opening \'$first_u_file\'
open -t "$first_u_file"
share|improve this question

migrated from stackoverflow.com Jun 4 '12 at 15:23

This question came from our site for professional and enthusiast programmers.

3  
echo "opening '$first_u_file'" - you don't need to escape the single quotes and the variable gets properly quoted. –  Dennis Williamson May 31 '12 at 11:16

4 Answers 4

up vote 3 down vote accepted

With GNU grep, you can try this (untested):

first_u_file=$(hg resolve -l | grep -m1 '^U')
first_u_file=${first_u_file#U }

-mX tells grep to stop after printing X lines, 1 in this case. The $(...) construct is similar to backticks, it turns output into a string. The second line removes "U " from the beginning of the file name.

share|improve this answer
    
where does this need to live? is this an alias? sorry for my minimal understanding!! –  Dori May 31 '12 at 9:14
    
@Dori You can now use the variable, as in open -t "$first_u_file" and so on. –  choroba May 31 '12 at 9:31
    
+1 great thanks! it works!! So whats going on here? If i enter the above first_u_file lines in bash it works, i tried to put them in bash_profile and it does not work. How can store this permenantly in some config file? –  Dori May 31 '12 at 9:45
    
@Dori What exactly do you need to keep stored? –  choroba May 31 '12 at 9:51
1  
@Dori: Functions and commands only return their exit status, not string. But you can create a script or a function that prints (echo-es) the value and then run open -t $(grab_first_file_name). –  choroba May 31 '12 at 10:26

Rather than just open the first file, your script could iterate over all unresolved files:

#!/bin/bash

while read; do
    file="${REPLY#U }"
    echo "Opening '$file'"
    open -t "$file"      
    hg resolve -m "$file"
done <( hg resolve -l | grep "^U" )

(I'm not familiar with hg; would hg resolve -aln give you just the list of unresolved files without the "U" prefix? That would do away with the need for the grep and the U-stripping step.)

share|improve this answer

A simpler solution:

u_file=$( hg resolve -l | awk '/^U/{print $2; exit}' )
share|improve this answer

hg resolve -l | egrep '^U.*' | head -n 1

the output is the first U file

to use it as open argument with "U" dropped:

firstUfile=`hg resolve -l | egrep '^U.*' | head -n 1 | sed -e 's/^U\s+\(.*\)$/"\1"/'`
open -t $firstUfile
share|improve this answer
    
+1 thanks, a good start. So how do I use this as an input for open -t. I need to remove the 'U ' also I presume –  Dori May 31 '12 at 9:11
    
I've edited to include U removal, I've put double quotes around the filename in case it has spaces, if you don't need it, feel free to remove from the sed substitution –  LeleDumbo May 31 '12 at 9:21
    
this does not seem to remove the 'U ' ? Do i jut put this in .bash_proile with a preceeding alias ? –  Dori May 31 '12 at 9:30
    
ah i see, its a variable, ok! executing that string of piped commands still gives me the preceeding U :( –  Dori May 31 '12 at 9:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.