Sponsored Content
Full Discussion: Scan Multiple Dir/Files
Top Forums UNIX for Dummies Questions & Answers Scan Multiple Dir/Files Post 302179901 by tonyd on Saturday 29th of March 2008 02:52:53 AM
Old 03-29-2008
Bug

Your right, I didn't give you much to go on. Here's what I came up with. Open to any suggestions based on your experience. Thanks!

tonyd
Code:
#!/usr/local/bin/ruby -w
require 'find'

@results = Array.new

# Iterate through the child directories & call the parse file method
def scan_dirs
	root = "/var/qmail/queue/mess"
	Find.find(root) do |file|
		parse_file(file)
	end
	# Sort on the second element in our array
	@results.sort! {|x, y| y[1] <=> x[1]}
	print_results
end

# Parse each file for the information we want
def parse_file(path)
	
	file =	path[(path.length-7), path.length]
	sourceip = ""
	email = ""
	subject = ""
	line_no = 0

	File.open(path, 'r').each do |line|
		
		line = line.strip # Remove any \n\r nil, etc
		line_no += 1
		
		if line_no == 1
			if line.match("invoked for bounce")
				# Internal Bounce Msg
				sourceip = "SMTP"
			end
		end
		
		if (line_no == 2 and sourceip.empty?)
			if line.match("webmail.internet.net")
				sourceip = "Webmail"
			else
				sourceip = line.scan(/\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/)
				if sourceip.empty?
					sourceip = "No Source IP**"
				end
			end
		end

		if (line.match("SquirrelMail") and sourceip == "Webmail") or
			 (line.match("From:") and sourceip != "Webmail")
			 if email.empty?
			 	  email = get_email(line)
			 end
		end

		if line.match("Subject:") and subject.empty? 
			subject = truncate(line,50)
		end

		if line_no == 20 #Nothing more we want to read in the file
		@results << ["#{file}", "#{sourceip}", "#{email}", "#{subject}"]
			line_no = 0
			return
		end
	end
end

# Truncate subject line
def truncate(string, width)
  if string.length <= width
    string
  else
    string[0, width-3] + "..."
  end
end

# Print out results
def print_results
	print "\e[2J\e[f"
	
	print "Mess#".ljust(10," ")
	print "Source".ljust(18," ")
	print "Email Addrress".ljust(30, " ")
	print "Subject".ljust(50, " ")
	1.times { print "\n" }
	111.times { print "-" }
	1.times { print "\n" }
	
	@results.each do |line|
		print line[0].ljust(10," ")
		print line[1].ljust(18," ")
		print line[2].ljust(30, " ")
		print line[3].ljust(50, " ")
	
		1.times { print "\n" }
	end
end

# Get email address from line/string
def get_email(line_to_parse)
	# Pull the email address from the line
	line_to_parse.scan(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i).flatten
end

# Ok, begin our scan
scan_dirs
exit

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripting help for rm log files from multiple dir

Hi, I'm quite new to scripting and need some help. I need to have one script that will check specific directories for files older than one month and then have the script delete them. I have written the script below but it only does one directory. I don't quite know how to make it so it... (7 Replies)
Discussion started by: morgadoa
7 Replies

2. Shell Programming and Scripting

How to copy specified files from list of files from dir A to dir B

Hello, fjalkdsjfkldsajflkajdskl (3 Replies)
Discussion started by: pmeesara
3 Replies

3. Shell Programming and Scripting

need to move files of particular day from one dir to another dir

Hi, I have hundered's of files of the name CMP_PORT_IN_P200903271623042437_20090328122430_err.xml in error directory of todays date ie 20090328 and in the file name 5th field specifies date only now i want to move all files of 20090328 to another directory i.e reprocess directory. So... (3 Replies)
Discussion started by: ss_ss
3 Replies

4. Shell Programming and Scripting

replace string in multiple files, dir and subdir

Hello, I have a directory www with multiple directories. Every directory has site name with .htm, .html, .php files or sub directories with .htm, .php, .html file as example - www - sitename 1 - site 1 - sitename 2 - sitename 3 What I'm looking for is a... (7 Replies)
Discussion started by: andyjill
7 Replies

5. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

6. Shell Programming and Scripting

find string from multiple dir and redirect to new files

Hi, I am new to script and I want find one string from multiple files in diff directories and put that out put to new file. Like I have A,B & C directories and each has multiple files but one file is unic in all the directories like COMM.txt Now I want write script to find the string... (8 Replies)
Discussion started by: Mahessh123
8 Replies

7. Shell Programming and Scripting

moving files from a dir in one machine to a dir in another machines

Hi, I am a unix newbie.I need to write a shell script to move my oracle READ WRITE datafiles from one serevr to another. I need to move it from /u01/oradata/W1KK/.. to /u01/oradata/W2KK, /u02/oradata/W1KK/.. to /u02/oradata/W2KK. That is, I actaully am moving my datafiles from one database to... (2 Replies)
Discussion started by: mathews
2 Replies

8. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

9. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

10. AIX

Assign read write permission to the user for specific dir and it's sub dir and files in AIX

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. I do not want to assign user the same group of that directories too.... (0 Replies)
Discussion started by: blinkingdan
0 Replies
gets(n) 						       Tcl Built-In Commands							   gets(n)

__________________________________________________________________________________________________________________________________________________

NAME
gets - Read a line from a channel SYNOPSIS
gets channelId ?varName? _________________________________________________________________ DESCRIPTION
This command reads the next line from channelId, returns everything in the line up to (but not including) the end-of-line character(s), and discards the end-of-line character(s). ChannelId must be an identifier for an open channel such as the Tcl standard input channel (stdin), the return value from an invocation of open or socket, or the result of a channel creation command provided by a Tcl extension. The channel must have been opened for input. If varName is omitted the line is returned as the result of the command. If varName is specified then the line is placed in the variable by that name and the return value is a count of the number of characters returned. If end of file occurs while scanning for an end of line, the command returns whatever input is available up to the end of file. If chan- nelId is in nonblocking mode and there is not a full line of input available, the command returns an empty string and does not consume any input. If varName is specified and an empty string is returned in varName because of end-of-file or because of insufficient data in non- blocking mode, then the return count is -1. Note that if varName is not specified then the end-of-file and no-full-line-available cases can produce the same results as if there were an input line consisting only of the end-of-line character(s). The eof and fblocked commands can be used to distinguish these three cases. EXAMPLE
This example reads a file one line at a time and prints it out with the current line number attached to the start of each line. set chan [open "some.file.txt"] set lineNumber 0 while {[gets $chan line] >= 0} { puts "[incr lineNumber]: $line" } close $chan SEE ALSO
file(n), eof(n), fblocked(n), Tcl_StandardChannels(3) KEYWORDS
blocking, channel, end of file, end of line, line, nonblocking, read Tcl 7.5 gets(n)
All times are GMT -4. The time now is 06:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy