Reputation: 37
What I am trying to do is kind of hard to explain. Let's say I have a directory with a bunch of different users submitting stuff into it. I ask for a file with the extension ".foo" from everyone. Then I asked for a file with the extension ".bar" from everyone. I want a list of names of everyone who submitted .foo, but did not submit .bar.
I'm assuming you somehow use grep twice and include the owner name inside the grep, but that's kind of where I get confused. I am trying to do this all in one command pipeline.
This is what I'm trying to do to retrieve the names:
find . -type f -printf "%u %f\n" | grep '%u .foo' | grep -v '%u .bar'
My two problems here are that %u
doesn't give the name from the printf
and that the excluding bar part isn't dependent off of grep foo. It's meant to list the names of all of the people that own foo, then remove the names of the people who have submitted bar.
Structure as per request:
|--myDirectory
├── samantha.foo - (owner: samantha warren)
├── samantha.bar - (owner: samantha warren
├── robert.foo - (owner: robert jones)
├── doug.foo - (owner: doug field)
├── doug.bar - (owner: doug field)
├── emily.foo - (owner: emily smith)
The output should be as follows:
robert jones
emily smith
Upvotes: 2
Views: 106
Reputation: 4887
It doesn't look like samantha.foo
is owned by samantha
in the sense of Linux file ownership. The ownership here seems to be within your application. If your filenames contain the owner name, you can try something like:
# Get all files with extensions foo and bar
foos=(*.foo)
bars=(*.bar)
# Strip the extensions to get the usernames
users_with_foo=("${foos[@]%.foo}")
users_with_bar=("${bars[@]%.bar}")
# Filter the usernames.
grep -vxf <(printf "%s\n" "${users_with_bar[@]}") <(printf "%s\n" "${users_with_foo[@]}")
With grep
, -v
prints lines that don't match any patterns, -x
looks for exact matches (the whole line must match the pattern) and -f
is used to read patterns from an input file. Here, the "files" are from process substitution (<(...)
). So the printf
commands' output are provided as files to grep
, which it uses as files for patterns and input.
Upvotes: 2