0

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!

Raffa
  • 32,237

2 Answers2

0

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

Stormlord
  • 6,317
  • 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. – Raffa Dec 17 '22 at 09:23
  • @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. – Stormlord Dec 17 '22 at 14:35
  • 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 :-) – Raffa Dec 17 '22 at 15:22
  • @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. :-) – Stormlord Dec 17 '22 at 15:48
0

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.

janos
  • 4,888
  • 1
    grep -onw 'chris butcher' people.txt would also print line numbers and avoid false matching e.g. chris butcherjunior as chris butcher. – Raffa Dec 17 '22 at 09:42