Solved

Updating a file in Unix

Posted on 2006-06-26
2
341 Views
Last Modified: 2010-04-21
Hi,

I need help in writing a shell script. I have a file Date.out that is being modified everyday.


-rw-r--r--+  1 STND10     STND10           10 Jun 22 09:40 Date.out


I required to create a shell script to capture the file modified date form Date.out & update another file datelog.out.

I have to update only Previous Date of the datelog.out file.

The datelog.out file have following  2 rows. Only the Previous Date to be updated.


Current Date  = 25-June-2005
Previous Date = 22-June-2005


Thanks,
Rao
0
Comment
Question by:srpendyala
2 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 16988044

It will vary some from one implementation of *nix to another, but his works on mine:

sed -n "/Current Date/p;/Previous/c\Previous Date = `ls -l --time-style=long-iso x|awk '{print $6 " " $7}'`\ " datelog.out > x$
mv x$ datelog.out


Good Luck,
Kent
0
 
LVL 27

Accepted Solution

by:
Nopius earned 500 total points
ID: 16989336
Kdo: not portable:
bash-3.00$ ls -l --time-style=long-iso
ls: illegal option -- time-style=long-iso
usage: ls -1RaAdCxmnlhogrtucpFbqisfHLeE@ [files]

srpendyala:

From manual (man ls) -l flag:
If  the  time  of  last  modification  is greater than six months ago, it  is  shown  in  the format `month date year' for the POSIX locale. When the LC_TIME locale category is not set to the POSIX locale, a different format of the time field can be used. Files modified within six months show  `month date  time'.

The problem is to calculate the year of modification of Date.out from 'ls -l' output.

Now is my script, it should be portable to any POSIX system. Before running it, please ensure, that you have newline character after 'Previous Date = 22-June-2005' in file datelog.out. Newline is importent for sed. So, your last line will be empty.

#!/bin/sh
# which file to test
FILE=Date.out
LOGFILE=datelog.out

NOW_YEAR=`date +%Y`
NOW_MON=`LC_TIME=POSIX date +%b`

# Get file's year, month and date
FILE_MON=`LC_TIME=POSIX ls -l $FILE | awk '{print $6}'`
FILE_DAY=`LC_TIME=POSIX ls -l $FILE | awk '{print $7}'`
FILE_YEAR=`LC_TIME=POSIX ls -l $FILE | awk '{print $8}'`

case $FILE_YEAR in
# It's a time, calculate year from current
  *:*)
        # If file month in previous year and current month in a new year - use previous year
        if ( echo "/Jul/Aug/Sep/Oct/Nov/Dec/" | grep $FILE_MON > /dev/null ) && ( echo "/Jan/Feb/Mar/Apr/May/Jun/" | grep $NOW_MON > /dev/null )
        then
          FILE_YEAR=`expr $NOW_YEAR - 1`
        else
          FILE_YEAR=$NOW_YEAR
        fi
        ;;
# if not a time, assume it's a year
esac

# Now we have FILE_MON in a short format 'Jan/Feb/.../Dec', convert it to long format
case $FILE_MON in
  Jan)
      FILE_MON_LONG=January
      ;;
  Feb)
      FILE_MON_LONG=February
      ;;
  Mar)
      FILE_MON_LONG=March
      ;;
  Apr)
      FILE_MON_LONG=April
      ;;
  May)
      FILE_MON_LONG=May
      ;;
  Jun)
      FILE_MON_LONG=June
      ;;
  Jul)
      FILE_MON_LONG=July
      ;;
  Aug)
      FILE_MON_LONG=August
      ;;
  Sep)
      FILE_MON_LONG=September
      ;;
  Oct)
      FILE_MON_LONG=October
      ;;
  Nov)
      FILE_MON_LONG=November
      ;;
  Dec)
      FILE_MON_LONG=December
      ;;
esac

# now we have correct FILE_YEAR, FILE_MON, FILE_DAY
cat $LOGFILE > $LOGFILE.tmp
( sed "/Previous Date/s/= .*\$/= $FILE_DAY-$FILE_MON_LONG-$FILE_YEAR/" < $LOGFILE.tmp > $LOGFILE ) && rm $LOGFILE.tmp
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now