Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Updating a file in Unix

Posted on 2006-06-26
2
Medium Priority
?
362 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 46

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 2000 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

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…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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 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…
Suggested Courses

971 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