Operating System - HP-UX
1748271 Members
4155 Online
108760 Solutions
New Discussion юеВ

Re: Column's format display challenge (scripting)

 
SOLVED
Go to solution
Jose Mosquera
Honored Contributor

Column's format display challenge (scripting)

i.e: when the command "cal 04 2009" is executed I need ensure display all days placed into 5th column (Th) command's output.
In this case the followin string must be caught in a variable:
2 9 16 23 30

The problem here are the left lead space characters. Some hint or solution for this?

Rgds.
5 REPLIES 5
mobidyc
Trusted Contributor

Re: Column's format display challenge (scripting)

Hello,

cal 04 2009 |tail +3 |cut -c13-14
2
9
16
23
30


Done.

BR,
Cedrick Gaillard
Best regards, Cedrick Gaillard
Dennis Handly
Acclaimed Contributor

Re: Column's format display challenge (scripting)

>The problem here are the left lead space characters.

awk can ignore those:
awk '
NR == 3 {print $(NF-2)} # adjust for short week
NR > 3 {print $5}' # adjust for short last week
Suraj K Sankari
Honored Contributor

Re: Column's format display challenge (scripting)

Hi,

you can do like this also

cal 04 2009 |tail +3|cut -c13-14 |awk '{ printf " " $0 }'

example

[user2@rspc521 user2]$ cal 04 2009 |tail +3|cut -c13-14 |awk '{ printf " " $0 }'
2 9 16 23 30 [user2@rspc521 user2]$

Suraj
James R. Ferguson
Acclaimed Contributor
Solution

Re: Column's format display challenge (scripting)

Hi:

@ Suraj: The problem with using 'cut' (or something like 'substr' in 'awk' is that you rely on column widths that are potentially non-portable. For instance, your solution works in HP-UX, giving Jose the Thursday dates. If run in AIX, however, you return Wednesday dates --- not nice.

Dennis's solution works nicely:

# DAYS=$(cal 04 2009|awk 'NR==3 {print $(NF-2)};NR>3 {print $5}')

# echo ${DAYS}
2 9 16 23 30

Regards!

...JRF...
Jose Mosquera
Honored Contributor

Re: Column's format display challenge (scripting)

Thanks everybody for your answers.
James, a state of the art solution!
Rgds.