Sponsored Content
Top Forums Shell Programming and Scripting Create file every year and log data to it Post 302974004 by mbak on Wednesday 25th of May 2016 12:30:58 AM
Old 05-25-2016
Code:
$ ./test.sh
++ date +%Y
+ year=2016
+ for f in '/tmp/*.log'
+ cat /tmp/2016.log
cat: /tmp/2016.log: input file is output file
+ rm /tmp/2016.log
+ for f in '/tmp/*.log'
+ cat /tmp/aaa.log
+ rm /tmp/aaa.log
+ for f in '/tmp/*.log'
+ cat /tmp/bbb.log
+ rm /tmp/bbb.log
+ for f in '/tmp/*.log'
+ cat /tmp/ccc.log
+ rm /tmp/ccc.log

This doesn't seem to work as expected, it is removing the dated file 2016.log in this case after giving message "input file is output file'whereas I'm looking to append to it. I'm trying to figure out how to append to multiple dated log files for eg...all aaa.log files should append to one aaa.2016.log, bbb.log files in bbb.2016.log file and so on.

---------- Post updated at 11:30 PM ---------- Previous update was at 06:42 PM ----------

Code:
#!/bin/ksh
YEAR=$(date "+%Y")
cd /tmp
if [ ! -d "${YEAR}_LOGS" ]; then
  echo " ${YEAR}_LOGS doesn't exist. Creating dir..." > /dev/null 2>&1
  mkdir /tmp/${YEAR}_LOGS > /dev/null 2>&1
else
  echo " ${YEAR}_LOGS directory exists" > /dev/null 2>&1
fi

for i in $(ls /tmp/ |grep old | awk -F "." '/aaa|bbb|cccc|dddd/ {print $1}'); do
  cat /tmp/${i}.log.old >> /tmp/${YEAR}_LOGS/${i}_${YEAR}.log
  rm /tmp/${i}.log.old > /dev/null 2>&1
done

for i in /tmp/*.log; do
  echo "Log being moved to old is $i ... \n"
  cp $i $i.old
  if [ $? -eq 0 ]; then
     rm $i
  fi
done

This is what I came up with and wondering if this could be made simpler or enhanced further, pls advise.

Last edited by mbak; 05-25-2016 at 02:31 AM..
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

how to know year create/modified files?

Dear all, how to know created file time, include the year created/last modified? if using #ls -al only month date and time Thank you. (5 Replies)
Discussion started by: blesets
5 Replies

2. UNIX for Dummies Questions & Answers

Create Year directory, date subdirectory and archive the file

Hi, After checking all the UNIX threads, I am able to come up with a solution so far. I am working on a shell script where it moves the files to a certain directory. The conditions to check are 1) Check if the file exists in the current directory. 2) Check if the destination directory... (2 Replies)
Discussion started by: madhunk
2 Replies

3. UNIX for Advanced & Expert Users

Create a file and enter data in columns

Hello!!!!!!!! I have an issue regarding inserting records in a file columnwise.Is it possible using awk/nawk script? Example: File1: 1|AAA|25|2 5|qqe|20|7 4|wer|31|81 I need to create a second file in which data can be inserted in a columnwise manner i.e. File2: AAA|25|1|2... (1 Reply)
Discussion started by: abhijeet1409
1 Replies

4. Solaris

Create file for group of data:

Hi folks, I have the following data.Any help is greatly appreciated. order File_name 7222245 7222245.pdf 7222245 7222245a.pdf 7222245 7222245b.pdf 7222245 7222245c.pdf 7222245 7222245d.pdf 7222250 ... (1 Reply)
Discussion started by: kumar444
1 Replies

5. Shell Programming and Scripting

Copy data form File A and Create File B

File A I have list of : ABCND1 ABCND2 ABCnd3 ABCnd4 I want file B like below Start+ S Pate=ABCND1 AAlo1 S Pate=ABCND1 Q1234 S Pate=ABCND1,P12345 (7 Replies)
Discussion started by: asavaliya
7 Replies

6. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

7. Shell Programming and Scripting

create txt file form data file

File A.txt LL07 LL07_B_1 20 LL85 LL85_A_1 40 LL85 LL85_B_1 40 LL85 LL85_C_1 30 LL37 LL37_A_1 60 LL37 LL37_B_1 20 LL37 LL37_C_1 50 I want cretae diffrent tex file base of above file Should be threee text file LL07.txt LL85.txt LL37.txt Eaach text file have below data... (2 Replies)
Discussion started by: asavaliya
2 Replies

8. Shell Programming and Scripting

From 2 files create 3rd file with uncommon data

Hi All, I have two files. File1 and File2. Want to create another file with all the records of File1 those are not present in File2. Please guide. Thanks in advanced. Anupam (3 Replies)
Discussion started by: Anupam_Halder
3 Replies

9. UNIX for Beginners Questions & Answers

Create file based on data from two other files

I have looked through several threads regarding merging files with awk and attempted using join however have been unsuccessful likely as I do not fully understand awk. What I am attempting is to take a csv file which could be between 1 and 15,000 lines with 5 colums and another csv file that will... (4 Replies)
Discussion started by: cdubu2
4 Replies
UNBUFFER(1)						      General Commands Manual						       UNBUFFER(1)

NAME
unbuffer - unbuffer output SYNOPSIS
unbuffer program [ args ] INTRODUCTION
unbuffer disables the output buffering that occurs when program output is redirected from non-interactive programs. For example, suppose you are watching the output from a fifo by running it through od and then more. od -c /tmp/fifo | more You will not see anything until a full page of output has been produced. You can disable this automatic buffering as follows: unbuffer od -c /tmp/fifo | more Normally, unbuffer does not read from stdin. This simplifies use of unbuffer in some situations. To use unbuffer in a pipeline, use the -p flag. Example: process1 | unbuffer -p process2 | process3 CAVEATS
unbuffer -p may appear to work incorrectly if a process feeding input to unbuffer exits. Consider: process1 | unbuffer -p process2 | process3 If process1 exits, process2 may not yet have finished. It is impossible for unbuffer to know long to wait for process2 and process2 may not ever finish, for example, if it is a filter. For expediency, unbuffer simply exits when it encounters an EOF from either its input or process2. In order to have a version of unbuffer that worked in all situations, an oracle would be necessary. If you want an application-specific solution, workarounds or hand-coded Expect may be more suitable. For example, the following example shows how to allow grep to finish pro- cessing when the cat before it finishes first. Using cat to feed grep would never require unbuffer in real life. It is merely a place- holder for some imaginary process that may or may not finish. Similarly, the final cat at the end of the pipeline is also a placeholder for another process. $ cat /tmp/abcdef.log | grep abc | cat abcdef xxxabc defxxx $ cat /tmp/abcdef.log | unbuffer grep abc | cat $ (cat /tmp/abcdef.log ; sleep 1) | unbuffer grep abc | cat abcdef xxxabc defxxx $ BUGS
The man page is longer than the program. SEE ALSO
"Exploring Expect: A Tcl-Based Toolkit for Automating Interactive Programs" by Don Libes, O'Reilly and Associates, January 1995. AUTHOR
Don Libes, National Institute of Standards and Technology 1 June 1994 UNBUFFER(1)
All times are GMT -4. The time now is 02:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy