BobbyJackson
BobbyJackson

Reputation: 23

Match output results with text file

So i would like to know, how am i able to match an output results with the text file content. For example i have an output like these:

admins
workers
example
devs
back-office

and then i have a text file (groups-id.txt) which containing something like this:

admins,1
apple,2
sample-text,3
workers,4
example,5
devs,6
back-office,7
hr-admins,8

I try to grep like this. But it will contain the hr-admins group aswell because of admins word.

grep -Ew -i "admins|workers|example|devs|back-office" groups-id.txt 

How am i able to grep these words with there numbers(id)?

As of results i would like to have something like this:

admins,1
workers,4
example,5
devs,6
back-office,7

Upvotes: 2

Views: 149

Answers (4)

Paul Hodges
Paul Hodges

Reputation: 15438

You're already using extended matching. Just specify the patterns start at the beginning of the line.

$: grep -Ew -i "^(admins|workers|example|devs|back-office)" groups-id.txt
admins,1
workers,4
example,5
devs,6
back-office,7

You might also want to save yourself the trouble of wondering how grep is going to parse "words" with -w and just explicitly include the following delimiter.

$: grep -Ei "^(admins|workers|example|devs|back-office)," groups-id.txt
admins,1
workers,4
example,5
devs,6
back-office,7

Upvotes: 1

Zartaj Majeed
Zartaj Majeed

Reputation: 510

You'll need to create patterns that match the entire first field - so the key word "admins" needs to become the pattern "^admins,"

Say the keys are in a file called keys.txt

admins
workers
example
devs
back-office

Then use sed to create the patterns on the fly and feed to grep using bash process substitution

grep -f <(sed 's/^/^/; s/$/,/' keys.txt) groups-id.txt

gives

admins,1
workers,4
example,5
devs,6
back-office,7

Upvotes: 0

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10133

A one-liner for this question, using only join and sort utilities, would be

join -t, <(sort groups-id.txt) <(sort groups)

where the contents of the file groups is

admins
workers
example
devs
back-office

and that of the file groups-id.txt is as in the question, the output is

admins,1
back-office,7
devs,6
example,5
workers,4

The only caveat is that the order is different than that of the original files; output is ordered by group names. It can be ordered numerically by group ids:

join -t, <(sort groups-id.txt) <(sort groups) | sort -n -t, -k2

Output of the above is

admins,1
workers,4
example,5
devs,6
back-office,7

If names of selected groups are produced by a command then simply replace the sort groups with command_for_group_names | sort.

Upvotes: 0

dawg
dawg

Reputation: 104111

With awk:

$ awk -F, 'NR==FNR{f1[$1]; next} $1 in f1' file1.txt file2.txt 
admins,1
workers,4
example,5
devs,6
back-office,7

Explanation:

-F,              # set the field separator to a comma
NR==FNR          # idiomatic awkism that tests if this is the first file
  {f1[$1]; next} # if so, store that word into an associative array
$1 in f1         # if the first field of second file has been seen
                 # then print the record

Upvotes: 0

Related Questions