Grep or Cat Cut Value Used in Subsequent Grep of Cat "criteria" Statement

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Jator
Level 2
Level 2
Posts: 80
Joined: Sat Mar 13, 2021 10:58 am

Grep or Cat Cut Value Used in Subsequent Grep of Cat "criteria" Statement

Post by Jator »

Say I have a file with the following data

Code: Select all

Tina
Steve
Mary
I would like to be able to pull one record, say Name 2 'Steve'

Code: Select all

cat /pathtofile/name.txt | head -2 | tail  -1
And use 'Steve' in a subsequent extraction from the following data:

Code: Select all

Steve	12
Mary	14
Tina		18
The code I imagine would look something like this, but not sure what goes within the "?" section of the code (or do you have to somehow assign Steve to a value that is referenced in a subsequent search? Tried Googling various questions to find a solution but couldn't find anything, hence asking the community.

Code: Select all

cat /pathtofile/name.txt | head -2 | tail  -1 | cat/pathtofile/records.txt | grep "?"
TIA
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Grep or Cat Cut Value Used in Subsequent Grep of Cat "criteria" Statement

Post by rene »

For a good answer the problem needs a more functional specification. I.e., as to the "-2" thing it would seem you know you are looking for Steve, so why not grep Steve data.txt? Is the issue that the data file might contain more or other names but you need all from name.txt? Then something like for NAME in $(cat name.txt); do grep "$NAME" data.txt; done. Are the name.txt and data.txt file guaranteed to have the same names even if in possibly different order? If so, sort is going to be part of the best answer.

I.e., needs more info...
Jator
Level 2
Level 2
Posts: 80
Joined: Sat Mar 13, 2021 10:58 am

Re: Grep or Cat Cut Value Used in Subsequent Grep of Cat "criteria" Statement

Post by Jator »

Thanks for the response. I'm pulling info from an html file, and the order (and perhaps names) will change on occasion, thus why I wanted to use the first grep results to then search in the second string from a different data source.

I'm creating a dashboard in conky and using behind the scene scripts to create a file to "pull" the data from in order. Thus on Monday, the order of results may be "Mary > Bill > Frank > Tina) and then on Tuesday it may be {Tina > Ahmed > Tarsha > Bill). If I am showing say the top 3, I was using the head -? | tail -1 to pull individual records to display.

I have the structure in place through separate scripts, but if I can have the grep result from the first file be used in the grep criteria in the second, I can collapse the number of steps that have to be done.

Hope that helps give more context.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Grep or Cat Cut Value Used in Subsequent Grep of Cat "criteria" Statement

Post by rene »

I'm still not completely clear on what's in the end the idea. For your example data file

Code: Select all

$ cat data.txt 
Steve	12
Mary	14
Tina	18
it seems that Tina > Mary > Steve, right? I.e., I suppose that's the "score" file and I suppose the idea is to display them in that order. So why is not simply sorting the data file by the score the answer? (edit: and/or limiting display to the first three lines of that result if the issue is that the data file contains more; stick a | head -3 after the sort). That is,

Code: Select all

$ sort -k2 -r data.txt 
Tina	18
Mary	14
Steve	12
or if without the scores themselves,

Code: Select all

$ sort -k2 -r data.txt | cut -f1
Tina
Mary
Steve
or if e.g. on one line,

Code: Select all

$ sort -k2 -r data.txt | cut -f1 | xargs
Tina Mary Steve
or....

Well, endless possibilities. If sort is not the answer then I still don't get it (enough); don't see where/how the separate name.txt comes into things.
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

Re: Grep or Cat Cut Value Used in Subsequent Grep of Cat "criteria" Statement

Post by FreedomTruth »

Jator wrote: Sat Jul 03, 2021 8:45 am

Code: Select all

cat /pathtofile/name.txt | head -2 | tail  -1 | cat/pathtofile/records.txt | grep "?"
grep $(readline 2 name.txt) records.txt

or, if you really like head and tail,
grep $(cat name.txt | head -2 | tail -1) records.txt
Locked

Return to “Scripts & Bash”