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

$ oslevel
$ 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


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
    The command oslevel and its output of make that look a lot like an AIX system, not a Solaris one. – Jeff Schaller Jul 26 '17 at 21:19

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'


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

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


If the ksh is available:

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


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);'

$ 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.

| improve this answer | |
  • 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"] – user3519878 Jul 26 '17 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. – Isaac Jul 26 '17 at 23:17
  • @user3519878 Added a Bash based solution, it may be useful. – Isaac Jul 27 '17 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) – Stéphane Chazelas Jul 27 '17 at 7:48
  • I should have tested it before posting. I believe it is working now. Thanks @StéphaneChazelas – Isaac Jul 27 '17 at 7:57

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))
if [ "$m" -lt 10 ]; then
  m=0$m # 0-pad single-digit numbers
sed "1s/$/MHHHRGFILE $y${m}01 99991231/" < file > newfile
| improve this answer | |
  • 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. – user3519878 Jul 27 '17 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. – user3519878 Jul 27 '17 at 18:12
  • Your function fails to add a required 0 to months under 10. Probably a missing printf '%02d' "$m" – Isaac Aug 1 '17 at 5:26
  • @Arrow, well spotted, thanks. Should be fixed now. – Stéphane Chazelas Aug 1 '17 at 7:34

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.


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

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

echo "$m"
echo $prvdt
a='MHHHRGFILE '"$prvdt"' 99991231'
echo $a

echo $var
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.

| improve this answer | |
  • The calculation fails to add a leading 0 if the resulting month is 9 or less. – Isaac Aug 1 '17 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.