Score:0

Ubuntu Bash - print specific word from a file

cc flag

I need some help with some bash. I've only learnt like grep, awk, find, sed etc.

Say I have a file named people.txt with different names on a line on multiple lines but a name appears in every line. For example:

Ln1 - chris butcher, sam witwickie, joseph stalin, king kunta

Ln2 - Thor, ironman, mariah carey, chris butcher

Ln3 - jen love, chris butcher, jeep lake

How would I print chris butcher's name on each line? Note that chris butcher's name appear on different parts of the line

The expected result I would like is:

Ln1 - chris butcher

Ln2 - chris butcher

Ln3 - chris butcher

I know that grep -i "chris butcher" would highlight the names in each line but I need it to print just 'chris butcher' in each line as the result.

Thanks in advanced!

hr flag
Why do you need to do this in bash specifically? If you know `grep`, you are presumably aware of its `-o` or `--only-matching` option?
Raffa avatar
jp flag
Related post: [Can globbing be used to search file contents?](https://askubuntu.com/q/1445515/968501)
Score:0
ru flag

This bash script should do the job:

#!/bin/bash

fileName="/path/to/file.txt"
identifier="chris butcher"
counter=1
while read oneLine; do
    if [[ "$oneLine" == *"$identifier"* ]]; then
        echo "Ln$counter - $identifier"
        ((counter++))
    fi
done < "$fileName"
echo
Raffa avatar
jp flag
What if `Ln2 - …` doesn’t contain `chris butcher` or empty newlines exist? … Then, your counter would be wrong :-) … What if `chris butcherjunior` exists in a line? .., Then, text globbing would print false result … Probably it would be more accurate if you use the `=~` operator and increment the counter outside the `if` statement and split names by e.g. `,` before hand.
ru flag
@Raffa, The script fullfills the parameters the question has set. The parameters you set are new. The counter will increase only if **chris butcher** exists in the specific line defining the line numbers it exists only. The question never stated that the counter should increase anyway. Neveltheless, the user who asked the question doesn't seem to care about any answer since they haven't shown up since they posted the question.
Raffa avatar
jp flag
I totally agree with you that the examples and parameters set in the question are less than adequate/clear … The limitations, however, of a given solution need to be clearly stated as possible to prevent misconception for the OP or future readers … I guess you agree with that :-)
ru flag
@Raffa, yes, you are right. Let's hope the OP will state what they want more accurately so that the script can be modified accordingly. :-)
Score:0
in flag

Given this input file:

Ln1 - chris butcher, sam witwickie, joseph stalin, king kunta
Ln2 - Thor, ironman, mariah carey, chris butcher
Ln3 - jen love, chris butcher, jeep lake

Using grep -o 'chris butcher' people.txt will output:

chris butcher
chris butcher
chris butcher

As @Raffa suggested in a comment, you might also want to make the condition to be more strict, match word expressions with -w, to avoid false matches such as "chris butcherfoo" or "foochris butcher". Also, your example had some line numbers, so the -n might also be interesting for you.

Raffa avatar
jp flag
`grep -onw 'chris butcher' people.txt` would also print line numbers and avoid false matching e.g. `chris butcherjunior` as `chris butcher`.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.