Solved

Updating a file in Unix

Posted on 2006-06-26
2
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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 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…
Suggested Courses

734 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