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.

I have a number of files with this pattern:

file1_foo.ext
file1_bar.ext
file2_foo.ext
file2_bar.ext

and so on.

I need to "pair them" and put them in a file like this

file1_foo.txt    file1_bar.txt

(tab separated)

What's the best course of action in this case?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

I assume you don't want to just assume both exist. This will only print the lines if they both exist.

for file1 in *_foo.ext; do
    file2="${file1%foo.ext}bar.ext"
    if [[ -e "$file2" ]]; then
        printf '%s\t%s\n' "$file1" "$file2"
    fi
done

Sample:

$ tee {1,2,3}_foo.ext {1,3,4}_bar.ext </dev/null >/dev/null
$ ./script
1_foo.ext   1_bar.ext
3_foo.ext   3_bar.ext
share|improve this answer
    
Right, I forgot about % and extensions. I'll test this at once. –  Einar Apr 3 '13 at 8:55

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.