File processing one after another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File processing one after another file
# 8  
Old 06-25-2014
This is my actual script this processing my Apache logs spiting depends on CNAME and SCODE. what happening now copying all file from Raw_dir to Processing_dir and then splitting the logs depending on scode value after that zipping and taking backup of which is processed.

Required format is: I need to take one file from Raw_dir copy to processing_dir then split and zip and moved to processed_dir.

Then take 2nd file do the above process up to available files(do not process before processed from Raw_dir)

Code:
#!/bin/sh

echo ======= LogManageri start ==========

#This directory is getting the raw data from remote server

Raw_data=/opt/ftplogs

# This directory is ready for process the data

Processing_dir=/opt/processing_dir

# This directory is prcoessed files and taking backup

Processed_dir=/opt/processed_dir

# This directory spliting the files like access and error logs

split_dir=/opt/split_dir

# Copying Raw data to Processing directory

echo starting copying files from $Raw_Data to $Processing_dir

cp -p $Raw_data/*.gz $Processing_dir/

echo done copying raw files.

# Decompress .gz files from $Process_dir

echo starting unziping files in $Process_dir

gunzip $Processing_dir/*.gz

echo done unziping files in $Process_dir

# This for loops gives year,month and day from file name

for file in $Processing_dir/*.log
do
        #Name of the input file

        name=$(basename "$file" .log)
        echo this $name

        #getting year from file name

        year=${name:9:4}

        echo $year

        #getting month from file name

        month=${name:13:2}

        echo $month

        #getting day from file name

        day=${name:15:2}

        echo $day

# This while loop reading the file each and every line and spliting the Respective $cname and $code

echo start reading files

while read -r line
do
        # cname is reading every line from all files

        cname=$(echo ${line} | awk '{split($11,c,"/"); print c[3]}')

        echo $cname

        # scode is reading every line from all files

        scode=$(echo ${line} | awk -F"[ ]" '{print $13}')

        echo $scode

        # creating directory if not exists

        [[ ! -d "$split_dir/$cname/$year/$month/$day" ]] && mkdir -p "$split_dir/$cname/$year/$month/$day"
        # this loop not processing starting with # line

        case $line in

        (\#*) continue

        esac

        # If condition scode less than 399 logs goes to access log
        if [ $scode -le 399 ]

        then

        echo ${line} >> $split_dir/$cname/$year/$month/$day/${name}_${cname}_access.log

        # If condition scode greater than 400 logs goes to error log

        else
#       [[ ( "${scode}"-ge"400" )]]

        echo ${line} >> $split_dir/$cname/$year/$month/$day/${name}_${cname}_error.log

        fi

done < $file

        done

echo files reading done

# after successfull splitting the the logs gzip .log file

echo starting zip logs

gzip $Processing_dir/*.log

echo compression done

# gzip file moves to $Processed directory

mv $Processing_dir/*.gz  $Processed_dir

echo moved file to $Processed_dir


Please suggest to me how do i change in my code.

Last edited by Franklin52; 06-25-2014 at 06:51 AM.. Reason: Please use code tags
# 9  
Old 06-25-2014
So:-
  • What's actually wrong?
  • What output do you get that is worrying you?
  • Why so many blank lines?
Are you looking to speed it up or something? With very large files you are calling your echo ......| awk ..... for every single line, which is very expensive.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with file processing

I have a fixed width file coming from source system. The total characters on each record is 786. I am getting records in the file with less than and greater than this number and the process is failing bcs of this length of the record Is there a way to spit out the bad records (length <>786)... (5 Replies)
Discussion started by: dsravanam
5 Replies

2. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

3. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

4. Shell Programming and Scripting

Recursive file processing from a path and printing output in a file

Hi All, The script below read the path and searches for the directories/subdirectories and for the files. If files are found in the sub directories then read the content of the all files and put the content in csv(comma delimted) format and the call the write to xml function to write the std... (1 Reply)
Discussion started by: Optimus81
1 Replies

5. Shell Programming and Scripting

processing an input file and then the output file

Hi. I am new to scripting and could really do with some advice on the best way to put a script together. Here is the scenario I am working to; - i will get files via ftp to a tmp directory on the server - all files will have a unique file name but with the same extension (.USM) - for each... (5 Replies)
Discussion started by: yabai
5 Replies

6. Shell Programming and Scripting

How to processing the log file within certain dates based on the file name

Hi I am working on the script parsing specific message "TEST" from multiple file. The log file name looks like: N3.2009-11-26-03-05-02.console.log.tar.gz N4.2009-11-29-00-25-03.console.log.tar.gz N6.2009-12-01-10-05-02.console.log.tar.gz I am using the following command: zgrep -a --text... (1 Reply)
Discussion started by: shyork2001
1 Replies

7. Shell Programming and Scripting

File processing

Hello, I am trying to find a solution which could be simple rather than coding unnecessary code. Scenario I have is :- I have a line sequential text file and while reading the file I need to call some kind of program which does data transfer, and read next so on... The issue I have is I have... (4 Replies)
Discussion started by: sentak
4 Replies

8. Shell Programming and Scripting

how to change the current file processing to some other random file in awk ?

Hello, say suppose i am processing an file emp.dat the field of which are deptno empno empname etc now say suppose i want to change the file to emp.lst then how can i do it? Here i what i attempted but in vain BEGIN{ system("sort emp.dat > emp.lst") FILENAME="emp.lst" } { print... (2 Replies)
Discussion started by: salman4u
2 Replies

9. Shell Programming and Scripting

Checking for a control file before processing a data file

Hi All, I am very new to Shell scripting... I got a requirement. I will have few text files(data files) in a particular directory. they will be with .txt extension. With same name, but with a different extension control files also will be there. For example, Sample_20081001.txt is the data... (4 Replies)
Discussion started by: purna.cherukuri
4 Replies

10. Shell Programming and Scripting

Have a shell script check for a file to exist before processing another file

I have a shell script that runs all the time looking for a certain type of file and then it processes the file through a series of other scripts. The script is watching a directory that has files uploaded to it via SFTP. It already checks the size of the file to make sure that it is not still... (3 Replies)
Discussion started by: heprox
3 Replies
Login or Register to Ask a Question