Multiple Grep Results - Formatting

Hello,
Perhaps someone here can help with this. I'd like to grep a plain text file for a word and output each line containing a word found to a seperate line instead of back to back.

Examples:

Basic command:
cat file.txt > grep -i CAT > results.txt

file.txt:
The cat said meow
The cat was hungry

results.txt:
The cat said meow The cat was hungry

desired results:
The cat said meow
The cat was hungry

Thanks to anyone who can help :slight_smile:

-Sys

Looks like you making simple things difficult or me interpreting them wrongly

grep -i CAT file.txt > results.txt

Yes, perhaps my example was a bit too simplified for the task. I usually try to truncate the details a bit with my posts so the information isn't too confusing for anyone trying to read it. Basically, the script finds more than one instance of the word "cat" and does return the output to a line each result, but I am trying to use the output for an HTML page and I'd like to format the output to be one result per line and it seems to be mashing it all together. My apologies for the confusion.

Hi,
I am really sorry, I still cannot get what you want. Has my solution provide the desired result.
Or are you looking at something else.

Can you give the source file and the output html file format that you want

thanks

UPDATE:

I did find one way of doing this successfully. I was able to limit my results to one line per instance found by running this on the output file:

cat myfile.txt | sed 's/$/<br>/' > myfile.txt

This adds the text "<br>" to the end of each line and works quite well. Hopefully somone else will find this useful.

If anyone has any other ideas, that would be cool as well. :slight_smile:

Basically if you run this command:

cat file.txt | grep -i CAT > myfile.html

on this file:

The cat says meow
The cat is hungry

And open this file in a web browser, it ends up putting the search results formatted back to back.

Like this:

The cat says meow The cat is hungry

Even if the file myfile.html looks like this:
The cat saws meow
The cat is hungry

So I ran the previous command to append "<br>" to the end of each line, so the web browser would seperate each line appropriately.

Again, I hope somone else finds my dilemma/resolution useful. I would still like to find a way of doing this in line with the grep command though, not after the file has been generated if anyone has any hints. :slight_smile:

Hope this hleps

/* this is my test file */
$ cat jnk.txt
cat drinks milk
there is capital CaT here
there is no C@ here :slight_smile:
the cat is hungry

/here is the command and the output/
$ awk '{if($0 ~ /[Cc][Aa][Tt]/) print($0 "<br>" ) }' jnk.txt

cat drinks milk<br>
there is capital CaT here<br>
the cat is hungry<br>

I would suggest using:

awk 'toupper($0) ~ "CAT" {print $0, "<BR>"}' file.txt