Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2010, 02:44 PM
butcher butcher is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2001
Location: In a constant state of turmoil
Posts: 858 butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 20 h 5 m 30 sec
Reputation Power: 81
Grep or sed or awk for file manipulation??

Let me *try* and explain what I'm trying to do, and please keep in mind aside from a little command line stuff I'm a beginner to any of what I'm asking about.

I have a directory structure something like this:
Code:
/usr/local/chatlogs/
    webserver1/
        1.chatlog
        2.chatlog
    webserver2/
        1.chatlog
        2.chatlog
    webserver3/
        1.chatlog
        2.chatlog

The 1 and 2 in the file name (1.chatlog) correspond to a specific room id and there's the possibility they will be different ones each time.

I'm writing a bash script (trying to anyway) that will scoop up the logs in each /webserver(n) directory and put them into a single file but according to the file name. So in this instance I would like the 3 1.chatlog files to end up all togehter in a single 1.chatlog file (and the same with the 2.chatlog files into 1 big 2.chatlog) that I can then parse line by line. I can handle the parsing part, but what I was hoping for help on was getting the files together into 1 big 1.chatlog and 2.chatlog.

The main problem is that I will never know exactly what files are in each /webserver(n) directory and I'm trying to pull off putting them in 1 correspondingly named big file without having to loop through each directory and figure out what's there.

I know I can do something like:
Code:
LOGFILES="/usr/local/chatlogs/*/*.chatlog"
FULLLOGFILE="/path/to/new/filename"
cat $LOGFILES | grep -v "^#" > $FULLLOGFILE

#do what I need to with contents of $FULLLOGFILE


but that doesn't keep them separated as I need them by the file name that I need for a room id reference.
What I'm hoping somebody here can tell me is if there's a 'variable' way that I can get each of the different numbered files into a corresponding large file with the same name. I'm hoping there's a way to do a back reference of some kind that I can catch the number in the start if the file name (1.chatlog) and use it in the same statement for when I jam everything into it. So what I'm thinking is something like:

cat /usr/local/chatlogs/*/(*).chatlog > /new/file/path/\1.chatlog

So that whatever was captured in the () in the first part of the statement would be used in the \1 in the back part of the statement for every n.chatlog that might be in any of the /webserver directories at that time.

Thanks to anybody that stuck around to get to this point, and I hope my explanation was clear enough to understand what I'm hoping to do.

Butcher
__________________
- Butcher -

Reply With Quote
  #2  
Old December 8th, 2010, 11:08 AM
crustymonkey's Avatar
crustymonkey crustymonkey is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Minneapolis, MN
Posts: 356 crustymonkey User rank is Corporal (100 - 500 Reputation Level)crustymonkey User rank is Corporal (100 - 500 Reputation Level)crustymonkey User rank is Corporal (100 - 500 Reputation Level)crustymonkey User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 13 h 38 m 46 sec
Reputation Power: 11
This short shell script should do the trick:
bash Code:
Original - bash Code
  1. #!/bin/sh
  2.  
  3. OUTDIR=outdir
  4. TOPDIR=/usr/local/chatlogs
  5. logFExt=chatlog
  6. tmpFile=$(tempfile)
  7. trap "rm $tmpFile" 1 2 15
  8.  
  9. # Make sure we have our output directory
  10. mkdir -p $OUTDIR
  11.  
  12. # First get the unique numbers
  13. uniques=$(find $TOPDIR -type f -name "*.$logFExt")
  14. for u in $uniques ; do
  15.     echo $u | sed -e 's#^.*/\([[:digit:]]*\)\.chatlog$#\1#' >> $tmpFile
  16. done
  17. uniqueNums=$(sort $tmpFile | uniq)
  18.  
  19. # Remove our tempfile
  20. rm $tmpFile
  21.  
  22. # Now that we have the unique numbers for the chatlog precursors in all the
  23. # directories, we will combine them into our outfile
  24. for num in $uniqueNums ; do
  25.     # Get all the relevant files to combine
  26.     toCombine=$(find $TOPDIR -type f -name $num.$logFExt | sort)
  27.     cat $toCombine > $OUTDIR/$num.$logFExt
  28. done

That does assume (based on your example) that there aren't spaces in file or directory names.
__________________
badger badger badger badger
badger badger badger badger
MUSHROOM MUSHROOM

Last edited by crustymonkey : December 8th, 2010 at 11:19 AM.

Reply With Quote
  #3  
Old December 10th, 2010, 06:51 AM
butcher butcher is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2001
Location: In a constant state of turmoil
Posts: 858 butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level)butcher User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 20 h 5 m 30 sec
Reputation Power: 81
That's BEAUTIFUL crustymonkey!

Thank you very much for the extra effort.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Grep or sed or awk for file manipulation??

Developer Shed Advertisers and Affiliates



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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap