The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
My ps -ef|grep command results are chopped off bsp18974 UNIX for Dummies Questions & Answers 1 08-14-2007 10:35 AM
List grep results slire UNIX for Dummies Questions & Answers 14 10-31-2006 11:42 AM
How to refine results of grep -p priceb Shell Programming and Scripting 2 06-28-2006 08:40 AM
grep sorting/formatting Jaken Shell Programming and Scripting 10 05-13-2006 02:36 PM
Question about formatting results TheCrunge UNIX for Dummies Questions & Answers 5 02-23-2005 08:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-25-2004
sysera sysera is offline
Registered User
  
 

Join Date: Feb 2004
Posts: 92
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

-Sys
  #2 (permalink)  
Old 03-25-2004
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
Looks like you making simple things difficult or me interpreting them wrongly

grep -i CAT file.txt > results.txt
  #3 (permalink)  
Old 03-25-2004
sysera sysera is offline
Registered User
  
 

Join Date: Feb 2004
Posts: 92
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 (permalink)  
Old 03-25-2004
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
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 (permalink)  
Old 03-25-2004
sysera sysera is offline
Registered User
  
 

Join Date: Feb 2004
Posts: 92
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.
  #6 (permalink)  
Old 03-25-2004
sysera sysera is offline
Registered User
  
 

Join Date: Feb 2004
Posts: 92
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.
  #7 (permalink)  
Old 03-25-2004
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
Hope this hleps

/* this is my test file */
$ cat jnk.txt
cat drinks milk
there is capital CaT here
there is no C@ here
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>
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0