grep sorting/formatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep sorting/formatting
# 1  
Old 05-11-2006
grep sorting/formatting

Hi,

I currently have many log files for different webpages inside the folder /log/. The program allows the user to enter a month and then the grep function searches for it, the command i used is below:

grep -c "$month" ./log/*.log

This works and i can ascertain the number of matches, but I cannot find out how to format the name of the .log and the number of matches into two seperate columns. Kind of like:

Log Name Matches
--------------- --------

soccerscore.log 40
soccerfaqs 20

Any ideas how to do this?

At the moment its like

./logs/soccerscore.log:40

-Thanks for any help
# 2  
Old 05-11-2006
Use sed for this.
Code:
grep -c "$month" ./log/*.log | sed 's/\.\/log\///g' | | sed 's/:/ /g'

# 3  
Old 05-11-2006
or awk
Code:
grep -c "$month" | awk -F: {print $1,"   ",$1}'

# 4  
Old 05-12-2006
Quote:
Originally Posted by Ambikesh
Use sed for this.
Code:
grep -c "$month" ./log/*.log | sed 's/\.\/log\///g' | | sed 's/:/ /g'

It just does the same output as it does before when I use this.

The other awk one doesnt display the hits, it just displays the directory of the wepages in two columns.
# 5  
Old 05-12-2006
Code:
grep -c "$month" ./log/*.log | { read a; echo ${a##*/} ; } | sed -e "s_\(.*\):\(.*\)_\1 \2_g"

# 6  
Old 05-12-2006
Code:
 grep -c "$month" ./log/*.log | sed 's/\.\/log\///g' |  sed 's/:/ /g'

This very much works for me..
All that command does is to remove the "./log/" from the listing and replace ":" with " "
# 7  
Old 05-12-2006
Ah, sorry I must have missed a bit of the code, was doing it early in the morning ¬_¬.

Is there anyway to format that so the webpages are in one colum and the seperated hits in another?

Thanks for the help btw Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

GREP Formatting Question

I have the following nmap file output with the multiple IP’s listed in the format below. Is there a way that GREP can format the output to just display IP and any ports that contain 'http' for that IP on its own line? file: Host: 192.168.1.xxx () Ports: 80/open/tcp//http///,... (3 Replies)
Discussion started by: FCoda10
3 Replies

2. Shell Programming and Scripting

Formatting grep and awk output

Hi there. I have a very large file and I am trying to format it so that I can pull out certain pieces of data/info and report it in spreadsheet format/style. The file has ###### which will separate each line that will be listed in the spreadsheet. Whenever I find "No" at the end of a line I want... (7 Replies)
Discussion started by: kieranfoley
7 Replies

3. Homework & Coursework Questions

Shell Script: Sorting by column using grep/awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: You will write a script that will read a tab-separated file that contains the names of all 50 states &... (7 Replies)
Discussion started by: tburns517
7 Replies

4. UNIX for Dummies Questions & Answers

GREP for sorting

Hi , I have a file which has a large number of sequences of type: gi_1 AASSKSKSKSSKSKSK.... series_121 DDKFKFKGKGKH gi_3 FFFFLFFLFLFLF series_1 DFFFFFF pattern_3 GEEEEEEEEE gi_2 HKKGGKGKGK series_102 HHHHH pattern_1 (2 Replies)
Discussion started by: siya@
2 Replies

5. Shell Programming and Scripting

Formatting list of IP addresses into a grep command

Hi I have an input file with a list of random IP addresses, each on a new line. Below is just an example as I omitted the real IP addresses for obvious reasons. Input: random_ip.txt 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1... (7 Replies)
Discussion started by: lewk
7 Replies

6. Shell Programming and Scripting

AWK/GREP sorting help

hi everyone, I am kind of new to this forum. I need help in sorting this data out accordingly, I am actually doing a traceroute application and wants my AS path displayed in front of my address like this; 192.168.1.1 AS28513 AS65534 AS5089 AS5089 .... till the last AS number and if possible sort... (8 Replies)
Discussion started by: sam127
8 Replies

7. Shell Programming and Scripting

Output formatting of grep

hi, I am trying to find the word "root" from the follwoing sample file: #12.12.12.2222 echo "Hai........" 11.11.1.1111 3.23.AS.AA ab.cd.df.rf /usr/bin jhhh 12.12.AF.12 /urf/sss/kk kkk su sh.s exec su root; ,root sudo -sh cosrootex (3 Replies)
Discussion started by: flamingo_l
3 Replies

8. UNIX Desktop Questions & Answers

Grep result loses formatting

I am searching for a string in a file and then redirecting the contents in another file... however the formatting is not preserved.. Can you please help me on this ... (5 Replies)
Discussion started by: blackeyed
5 Replies

9. UNIX for Dummies Questions & Answers

Sorting using count, grep and count

Hi, I trying to sort information in a file by making count for every object. For example: A B A D A B C i would like to sort out count for each object (A's, B's and etc) but in actual case i dont know the object but i know the position ofthe object. do i need to use array as well? Any... (2 Replies)
Discussion started by: sukhpal_78
2 Replies

10. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: sysera
7 Replies
Login or Register to Ask a Question