Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Updating a file in Unix

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded‚Ķ
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

670 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