1

I am using Solaris O/S. Here are the basic stats for UNIX O/S I am using:

$ oslevel
7.1.0.0
$ ps
      PID    TTY  TIME CMD
 10944546  pts/1  0:00 -ksh
 17563840  pts/1  0:00 ps
$

I am need to write a text on the first line of a text file

test.txt

Text I need to add to the first line on test.txt is

MHHHRGFILE (sysdate - First day of 9 months) 99991231

For middle part (sysdate - First day of 9 months), for eg. Today's date is 07/26/2017, I will need to minus 9 months and then first day of that month. Meaning, I will need to print 20161001

Basically I will end up writing:

MHHHRGFILE 20161001 99991231

to the first line of the file test.txt where MHHHRGFILE and 99991231 are staying constant all the time.

1
  • 1
    The command oslevel and its output of 7.1.0.0 make that look a lot like an AIX system, not a Solaris one.
    – Jeff Schaller
    Jul 26, 2017 at 21:19

3 Answers 3

2

Solaris/Aix date

Sadly, there is no solution possible with (only) Solaris/Aix date.

GNU date

If gnu date is available:

Build the date needed in a variable:

$ date +'%Y/%m/01 -9 months'
2017/07/01 -9 months
$ a=$(date +'%Y/%m/01 -9 months')
$ a=$(date -d "$a" +'%Y%m%d')
$ a='MHHHRGFILE ('"$a"') 99991231'
$ echo "$a"
MHHHRGFILE (20161001) 99991231

Or simply:

$ a=$(date -d "$(date +'%Y/%m/01 -9 months')" +'%Y%m%d')
$ a='MHHHRGFILE ('"$a"') 99991231'

Bash

If bash (4.2+) is available: (using 272 days as equivalent to 9 months)

$ TZ=UTC0 printf '%(%Y%m01)T' $(($(date +'%s') - 3600*24*272))
20161001

$ a=$(TZ=UTC0 printf '%(%Y%m01)T' $(($(date +'%s') - 3600*24*272)))

ksh93

If the ksh is available:

$ printf "%(%Y%m%d)T" "9 month ago"

Perl

If perl is available (9 months are aproximated with 3600*24*30*9):

$ perl -e 'use POSIX qw(strftime); print strftime "%Y%m01",localtime(time()- 3600*24*30*9);'
20161001

$ a=$(perl -e 'use POSIX qw(strftime); print strftime "%Y%m01",localtime(time()- 3600*24*30*9);')

Edit the file

Append that variable to the first line of file

$ sed '1s/.*/&'"$a"'/' file >file.new; mv file.new file

That's it.

5
  • Here is my script:#!/bin/ksh date +'%Y/%m/01 -9 months' a=$(date +'%Y/%m/01 -9 months') a=$(date -d "$a" +'%Y%m%d') echo "$a" it errors like this :$ ./add* 2017/07/01 -9 months date: Not a recognized flag: d Usage: date [-u] [+"Field Descriptors"] Jul 26, 2017 at 21:27
  • You do not have the GNU version of date. Your version of date does not have a -d option. Do you have an usable perl installed? Or, could you install a program in the computer ? Then install (and use) GNU date.
    – user232326
    Jul 26, 2017 at 23:17
  • @user3519878 Added a Bash based solution, it may be useful.
    – user232326
    Jul 27, 2017 at 6:27
  • No sed -i in AIX or Solaris. You can use perl -lpi -e '$_ .= $ENV{VAR}' if $. == 1' (GNU sed's -i was inspired from perl's) Jul 27, 2017 at 7:48
  • I should have tested it before posting. I believe it is working now. Thanks @StéphaneChazelas
    – user232326
    Jul 27, 2017 at 7:57
1

It's not complicated to do by hand here:

eval "$(date +'y=%Y m=%m')"
m=$((${m#0} - 9)) # need to strip the 0 for shells like bash or some 
                  # ash derivatives (but not ksh) that complain
                  # about an invalid 09 octal number in arithmetic
                  # expressions. Some date implementations support
                  # date +%-m, but that's not standard.
if [ "$m" -le 0 ]; then
  m=$((m + 12)) y=$((y - 1))
fi
if [ "$m" -lt 10 ]; then
  m=0$m # 0-pad single-digit numbers
fi
sed "1s/$/MHHHRGFILE $y${m}01 99991231/" < file > newfile
4
  • Thanks Stephane Chazelas It works till if loop and prints right characters but it is not writing to a file:( I got error message like this:file: 0403-016 Cannot find or open the file. Jul 27, 2017 at 17:54
  • Here is my script: #!/bin/ksh eval "$(date +'y=%Y m=%m')" m=$((${m#0} - 9)) if [ "$m" -le 0 ]; then m=$((m + 12)) y=$((y - 1)) fi echo "$m" prvdt=$y${m}01 echo $prvdt a='MHMRFILE '"$prvdt"' 99991231' echo $a sed '1s/.*/&'"$a"'/' file >filename_I_want_to_enter_This_text.txt Errors: sed: 0602-419 Cannot find or open file file. Jul 27, 2017 at 18:12
  • Your function fails to add a required 0 to months under 10. Probably a missing printf '%02d' "$m"
    – user232326
    Aug 1, 2017 at 5:26
  • @Arrow, well spotted, thanks. Should be fixed now. Aug 1, 2017 at 7:34
0

Here is the final script that works and outputs Text and Date (go back 9 months and picks first day of that month) into a file on "solaris AIX" Kshell.

#!/bin/ksh

eval "$(date +'y=%Y m=%m')"
m=$((${m#0} - 9))

if [ "$m" -le 0 ]; then
  m=$((m + 12)) y=$((y - 1))
fi

echo "$m"
prvdt=$y${m}01
echo $prvdt
a='MHHHRGFILE '"$prvdt"' 99991231'
echo $a


var="$a"
echo $var
destdir=/applic/e396/data/NNA/Dclients_for_FPD.txt
echo 'Destination File is locate at' $destdir

echo $var >> $destdir

The only issue I am having it add's text MHHHRGFILE 20161001 99991231 at the end of file while i need to add it at very top as a first line item.

Please let me know/help my code correctly so It writes output into destination file's first line.

1
  • The calculation fails to add a leading 0 if the resulting month is 9 or less.
    – user232326
    Aug 1, 2017 at 5:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .