Ubuntu – Print word containing string and first word

command lineregextext processing

I want to find a string in a line of text and print the string (between spaces) and the first word of the phrase.

For example:

"This is a single text line"
"Another thing"
"It is better you try again"
"Better"

The list of strings is:

text
thing
try
Better

What I am trying is to obtain a table like this:

This [tab] text
Another [tab] thing
It [tab] try
Better

I tried with grep but nothing occurred.
Any suggestion?

Best Answer

Bash/grep version:

#!/bin/bash
# string-and-first-word.sh
# Finds a string and the first word of the line that contains that string.

text_file="$1"
shift

for string; do
    # Find string in file. Process output one line at a time.
    grep "$string" "$text_file" | 
        while read -r line
    do
        # Get the first word of the line.
        first_word="${line%% *}"
        # Remove special characters from the first word.
        first_word="${first_word//[^[:alnum:]]/}"

        # If the first word is the same as the string, don't print it twice.
        if [[ "$string" != "$first_word" ]]; then
            echo -ne "$first_word\t"
        fi

        echo "$string"
    done
done

Call it like so:

./string-and-first-word.sh /path/to/file text thing try Better

Output:

This    text
Another thing
It  try
Better
Related Question