Visit Our UNIX and Linux User Community


Keeping the format ...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Keeping the format ...
# 1  
Old 01-31-2002
Keeping the format ...

Hi all,

Am trying to execute a loop but having some troubles...
Files that will be query'd use the Julian date (eg: cpu032, cpu365) in their naming convention.

I'm a little lost how to maintain the three character format of the numeric portion of the file name while cycling backwards(or forwards for that matter) in a loop.

Something similar to the following:
Code:
xstart=`date '+%j'`
xend=`expr $xstart - 30`

while [ $xstart -gt $xend ] ; do

  echo $xstart
  xstart=`expr $xstart - 1`

done

If $xstart begins at 032 (1-Feb) and $xend becomes 002 (2-Jan) then I should see...
032
031
030
029
...
004
003
002

Unfortunately I'm not - and is only displaying the significant digits.
Is there any way around this??
# 2  
Old 01-31-2002
Here's one that might help:

xstart=`date '+%j'`
xend=`expr $xstart - 30`
while [ $xstart -gt $xend ] ; do
if [ $xstart -lt 10 ]; then
echo 00"$xstart"
elif [ $xstart -lt 100 ]; then
echo 0"$xstart"
else
echo $xstart
fi
xstart=`expr $xstart - 1`
done

It's not the prettiest, but it does the trick.
# 3  
Old 01-31-2002
Thanks seismic_willy,

I've already use the same techique in another script for reporting stats, but thought that there may have been another way.

Again, thanks. Smilie
# 4  
Old 01-31-2002
With ksh, just use "typeset -Z3 xstart" at the top of the script. Then ksh will maintain the leading zeros.
# 5  
Old 02-01-2002
Thanks Perderabo,

Is there a similar way to do the same in `bash` ?
# 6  
Old 02-01-2002
Bash does not support that option to typeset.
Also, if you're using a Linux-based OS, you most likely have PDKSH installed, instead of ksh. Most versions of pdksh do not support that either - only the "real" ksh can be trusted...

Good news though! You can get David Korn's KSH 93 from here:
http://www.research.att.com/sw/download/

Using AT&T's package is a bit of a pain, but it's worth it.
# 7  
Old 02-01-2002
If you cannot use typeset, you can format it with awk:

Code:
#!/bin/sh

xstart=`date '+%j'`
xend=`expr $xstart - 30`

while [ $xstart -gt $xend ] ; do

  xname=`awk 'BEGIN {printf "%3.3d",'$xstart';exit}'`
  echo $xname
  xstart=`expr $xstart - 1`

done

Jimbo

Previous Thread | Next Thread
Test Your Knowledge in Computers #633
Difficulty: Medium
The touch and gesture features of the iPhone are based on technology originally developed by FingerWorks.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace some strings keeping others

I want to replace strings in test2 according to test1 table. In doing so, I`m losing records that I dont need to replace, please suggest modifications. what i have $ cat > test1 a b c d   $ cat > test2 a a a d d   what i tried $ awk ' BEGIN {FS=OFS=" "} FNR==NR{a=$2;next}... (2 Replies)
Discussion started by: senhia83
2 Replies

2. Shell Programming and Scripting

Keeping the number intact

Currently I have the following to separate the numeric values. However the decimal point get separated. ls -lrt *smp*.cmd | awk '{print $NF}' | sed 's/^.*\///' | sed 's/\(*\)/ & /g' As an example on the files n02-z30-dsr65-terr0.50-dc0.05-4x3smp.cmd... (8 Replies)
Discussion started by: kristinu
8 Replies

3. Shell Programming and Scripting

Replace date value with another value keeping all as is

Hi forum. How do I change the following date value with another value (while keeping the rest of the line) using sed? The date values can change so I need a general sed command to change the date value within the first quotation marks only. Date values will be coming from 2 different files.... (2 Replies)
Discussion started by: pchang
2 Replies

4. Shell Programming and Scripting

Keeping last part

Hello, Sorry for the poor tilte but I still don't know how to this. Here is my problem. I have to huge log file. In this log file I can know where is stored all my files. As I have to get a reporting of of files I only need to keep the file name but I don't know how to do it. I hope you... (5 Replies)
Discussion started by: Aswex
5 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

6. Solaris

keeping a process alive ?

Hello guys, I have one script running that I need to keep it running 24x7 so I'd like to know how can I implement a sort of monitoring process I mean if for some reason this process dies somehow it gets automatically started again. Thanks. (8 Replies)
Discussion started by: cerioni
8 Replies

7. Red Hat

keeping systems updated

I have several RHEL systems that are on an isolated network so I can't run up2date or yum directly on them. What is the best way to keep these systems updated and patched? Thanks (4 Replies)
Discussion started by: wazzu62
4 Replies

8. AIX

Script Keeping Track of Itself

Hi All We have a WEB Based application running on the IBM AIX server. There is a EOD Job which runs a UNIX script containing EOD Jobs. Say If any job fails then we have to explicitly comment out the jobs which were successfully executed and then re run the same. Is there any was by which we... (7 Replies)
Discussion started by: Prashantckc
7 Replies

9. Solaris

keeping history of command

hi can anyone tell me how or where to set to enable history of command keyed in to be logged? so that it can be used or traced later. thanks (3 Replies)
Discussion started by: legato
3 Replies

Featured Tech Videos