how to append line of of data to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to append line of of data to file
# 1  
Old 04-07-2009
how to append line of of data to file

hai..i am new to unix..and i've currently learn shell script..
i have this small problem where i would like to save every data from log file into user directory if the data is equal to the name of the user..
i manage to do that with below script..
i would like to ask if there is any solutions so that each user's log file would not take up too much space?like creating another new log file every time the file rotated?
i consider append before but when i do the read and print the output of the appending log file,i only get the latest data..(of course it is because the log file append)
below is the code that i made to split up every users log into their own directory..i will appreciate to consider any changes/opinion/suggestion given..
any solutions will be so much help..thank you Smilie

Quote:

$accessfile = "/path/foo/access_log";
open (example7, "/path/foo/access_log") || die ("Could not open file. <br> $!");
@cisco = <example7>;
$new = qx(ls /path/foo/users/) or die "Could not read file. <br> $!";

@passwd = split(' ',$new);

open(READER, $accessfile) or die "canot read log:$accessfile:$!";
@LOGFILE = <READER>;

while(<READER>) {
($machine, $date, $time, $tzone, $method, $url, $httpvers, $response, $size) = /([^\s]*) [^\s]* [^\s]* \[([^\:]*)Smilie[^\s]*) ([^\]]*)\] \"([^\s]*) ([^\s]*) HTTP\/([^\"]*)\" (\d*) (\d*)/;

($day, $month, $year) = split('/',$date);


}

foreach $string (sort(@passwd)) {

my $path = "/path/foo/foo/$string/";

if (! -d $path)
{
mkpath($path,755) or die "Failed to create $path: $!\n";
}

$file = "$string.txt";

if ($path =~ $string) {
foreach $line (sort(@cisco)) {
$append = 0;
if ($append){
open (OUTFILE,">$file"); if ($line =~ $string) {print OUTFILE $line;}
}else{
open (OUTFILE,">>$file"); if ($line =~ $string) {print OUTFILE $line;}
} }
close (OUTFILE);
move($file,$path) or die "Failed to copy $file: $!\n";
}
}
close (example7);
# 2  
Old 04-07-2009
a.) you can get the file size and other stuff using "lstat" then simply count
the line size once you start reading the file. Getting to the desired threshold,
start writing to a temp file. Overwrite the original.

b.) rotate the file log names using days of the week: log_Monday, log_Tuesday,
etc... Then the logs automatically remain a certain size and get overwriten.
This trick can be done using the days of the month, julian dates, whatever
seems appropriate for your archival needs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To append new data at the end of each line based on substring of last column

Hi guys, I need to append new data at the end of each line of the files. This new data is based on substring (3rd fields) of last column. Input file xxx.csv: U1234|1-5X|orange|1-5X|Act|1-5X|0.1 /sac/orange 12345 0 U5678|1-7X|grape|1-7X|Act|1-7X|0.1 /sac/grape 5678 0... (5 Replies)
Discussion started by: null7
5 Replies

2. Shell Programming and Scripting

awk to append data to a file

Hi I search for certain values in a file across many directories using the following awk code awk '/Sl.*thickness/ {Sl=$3;Tt=$NF}END{print Sl, Tt}' DILAT.DAT What I would like to do is write out Sl and Tt obtained from these files from many directories to a single file. So for example if... (2 Replies)
Discussion started by: lost.identity
2 Replies

3. Shell Programming and Scripting

append | to the end of each data in a file

I have a file which has data in the below format: 7810902|6783014102| || |0| |0| |0| |0|||||T|04/13/2006||9423|7421||100|2006-04-13 16:50:28|||2006-04-13 16:50:28|n|51|-1||214 1089929|||||NewSpCreateAction request successful. Activity ID = <826528>||||100|n|2006-04-13 16:50:27|2006-04-13... (3 Replies)
Discussion started by: ankianand88
3 Replies

4. Shell Programming and Scripting

I want to append data to same .csv file.

I have a script which has to be scheduled to run 3 times a day. My script picks the required fields from logfile and stores the data in a.csv file. Sample data. my logfile contain: 0097A,0374D,100903,1519,00000606191 0097A,C88RA,100903,0724,00000606105 So the output of first execution... (3 Replies)
Discussion started by: shrima.pratima
3 Replies

5. Shell Programming and Scripting

Match data based on two fields, and append to a line

I need to write a program to do something like a 'vlookup' in excel. I want to match data from file2 based on two fields (where both match) in file1, and for matching lines, add the data from two of the fields from file2 to file1. If anyone knows something in perl or awk that can do this, I'd be... (20 Replies)
Discussion started by: jamessmith01
20 Replies

6. Shell Programming and Scripting

want to append the data in one file to the another

Hi , i have two log files, i need to combine this as a one log file. i need to do this by SED , test1.log sadadadaadfsaf test2.log adadadadadada i need this in a single file from test 1 to test2.log test2.log(expected result) adadadadadada (7 Replies)
Discussion started by: mhdmehraj
7 Replies

7. Shell Programming and Scripting

append data to each line

Hi guys, I need to investigate a memory leak on a solaris server, so what I have done is pmap'd each process on the system with a script which tar'd the directory every hour in cron. Now I need to write a script to process the pmap data. So what I have is about 100 directories # ll... (2 Replies)
Discussion started by: borderblaster
2 Replies

8. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

9. UNIX for Dummies Questions & Answers

append data to file

i want to develop a script newdata that writes new data to a file called items the file items has the following headings columns separated by tabs: channel date time programe if i type executable file newdata on the command line with parameters, it should append it to the items files the... (1 Reply)
Discussion started by: fletcher
1 Replies

10. Shell Programming and Scripting

get the data from sybase and append to the file

How to get the data from the sybase database and append the data obtained into the file. For example, I will get the filename 'temp' from the database. This filename is associated with the number 1.6... This number 1.6 needs to be copied into the file that matches the filename in the... (1 Reply)
Discussion started by: vinay123
1 Replies
Login or Register to Ask a Question