Multiple Grep Results - Formatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple Grep Results - Formatting
# 1  
Old 03-25-2004
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 Smilie

-Sys
# 2  
Old 03-25-2004
Looks like you making simple things difficult or me interpreting them wrongly

grep -i CAT file.txt > results.txt
# 3  
Old 03-25-2004
Quote:
Originally posted by linuxpenguin
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.
# 4  
Old 03-25-2004
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
# 5  
Old 03-25-2004
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. Smilie
# 6  
Old 03-25-2004
Quote:
Originally posted by linuxpenguin
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
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. Smilie
# 7  
Old 03-25-2004
Hope this hleps

/* this is my test file */
$ cat jnk.txt
cat drinks milk
there is capital CaT here
there is no C@ here Smilie
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>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple Results of Grep in one Line/String?

Hello, I have a Textfile sees like this "Word1":aksdfjaksdf "Word2":askdfjalsdkfdlsjfasldfj "This is Word3":asdfkjalskdfj what i need is a string which sees like this Word1;Word2;This is Word3 Conclusion always the text within "" which is before the : i tried it with grep.... (10 Replies)
Discussion started by: SwordMaster
10 Replies

2. Shell Programming and Scripting

Cannot get results from grep command

Hi, i have a file hello.log which as several line that look like the below 2015-12-07 09:46:56 0:339 120.111.12.12 POST /helloWorld 2015-12-07 09:46:57 0:439 122.111.12.12 POST /helloWorld .... when i grep expecting to see results like the below. ... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. Shell Programming and Scripting

How to concatenate grep results?

hi, let's say we have input in files test1.txt, test2.txt, text3.txt ... ... ... ('...' means more files & lines not just 'dots') test1.txt has: A B C D ... ... ... test2.txt has A B C D ... ... ... (4 Replies)
Discussion started by: msonoth
4 Replies

4. Shell Programming and Scripting

Multiple results as input to grep or awk

Hi: This is my first post in this forum. apologies if I am asking question that has been asked here multiple times. I wrote a python script that prints list of identifiers. I have another file notoriously big and with no particular format. I plan to search the identifiers in this file... (2 Replies)
Discussion started by: oriolebaltimore
2 Replies

5. Shell Programming and Scripting

How to analyse results of grep

Hi all, I'm working with a peice of software that runs on Linux that allows planning trips in cars through maps. This software has different variations depending on the type of car, e.g. BMW, Audi, Hyundai, etc... Each variation has a dependency on common external components that are not... (1 Reply)
Discussion started by: emoshaya
1 Replies

6. UNIX for Dummies Questions & Answers

How to do ls -l on results of grep and find?

Hi, Am running the command below to search for files that contains a certain string. grep -il "shutdown" `find . -type f -mtime -1 -print` | grep "^./scripts/active" How do I get it to do a ls -l on the list of files? I tried doing ls -l `grep -il "shutdown" `find . -type f -mtime -1... (5 Replies)
Discussion started by: newbie_01
5 Replies

7. Shell Programming and Scripting

Grep no results

Hello guys, I have been looking around but can't find the answer to my problem: If the grep command displays no results, print "no results have been found" and increment x. But if the grep command find something, do nothing. if echo "no results have been found $x" x=`expr $x + 1 `... (3 Replies)
Discussion started by: Benou
3 Replies

8. UNIX for Dummies Questions & Answers

Formatting Multiple fields on 1 line to multiple rows

I'm trying extract a number of filename fields from a log file and copy them out as separate rows in a text file so i can load them into a table. I'm able to get the filenames but the all appear on one line. I tried using the cut command with the -d (delimiter) option but cant seem to make it... (1 Reply)
Discussion started by: Sinbad-66
1 Replies

9. UNIX for Dummies Questions & Answers

List grep results

Hi I need to search for matching strings in a database and I want to print out all files that matches in "detail", which means that I want the output to contain datum of last saving. I only get the grep function tp print the actual file names which is not enough since the database is to large... (14 Replies)
Discussion started by: slire
14 Replies

10. UNIX for Dummies Questions & Answers

Question about formatting results

OK, I have a command that is getting a result, that I am trying to format using awk. I think it's pretty ugly, and there is probably a better way to do it, but this is sorta working for me. Anyway, the command: cat /var/log/cups/page_log | grep testuser | grep My_office_printer | awk '{gsub... (5 Replies)
Discussion started by: TheCrunge
5 Replies
Login or Register to Ask a Question