Solved

Updating a file in Unix

Posted on 2006-06-26
2
334 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
Comment Utility

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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

762 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

7 Experts available now in Live!

Get 1:1 Help Now