Getting data with unstable space

Please notice the date output. the Oct 3, has 2 spaces after Oct.
i want to have an awk or sed that will get the exact space, data. Because if you are GREPING a syslog file. Date and spaces are very important, else you cant get the right data

$  date
Fri Oct  3 06:03:35 MST 2014

Output:
date|awk '{print $2,$3}'
Oct 3


I can achieve this by: 
$ date|awk '{print $2,"",$3}'
Oct  3

But the problem is, when october reach double digit,like october 10-31, below command are not working anymore

$ date|awk '{print $2,"",$3}'

because EXAMPLE TODAY is October 10

$date
Fri Oct 10 06:03:35 MST 201

this command will work perfectly fine
date|awk '{print $2,$3}'

But what awk and sed. will get what ever kind of date.

With printf you can use the width trick, something like:

date | awk '{ printf("%s %*s\n", $2, 2, $3) }'
1 Like

The logical way to get the abbreviated month and day is to use date by itself:

date '+%b %e'

If you insist on using awk :

date | awk '{printf("%s%3d\n", $2, $3)}'

If you insist on using sed :

date | sed 's/....\(......\).*/\1/'

And, if you like cut :

date | cut -c5-10
1 Like

OR else something like this might help

 date | awk '{split($0,d,/[^[:space:]]*/); printf("%s%s%s\n",$2,d[3],$3)}'

I am bit confused , forgive me if I misunderstood your requirement.

1 Like

Please note that there's no "unstable spaces". The format is very predictable as the 2 spaces after Oct are

  • the separator
  • the space padded day-of-month output of date .
    To avoid the space padding use another format string, e.g.
date +"%a %b %d %H:%M:%S %Z %Y"
Fri Oct 03 17:11:00 CEST 2014

as opposed to the default

date
Fri Oct  3 17:17:18 CEST 2014
1 Like

unfortunately..i cant change the date...so i will adjust my command..

the system works likes this..

$date
Oct   3

if 10 days above
$date
Oct 11

But this works for me.. thanks guys

awk '{ printf("%s %*s\n", $2, 2, $3) }'

---------- Post updated at 02:21 PM ---------- Previous update was at 02:21 PM ----------

awesome sir...

---------- Post updated at 02:23 PM ---------- Previous update was at 02:21 PM ----------

this works as well..thanks sir...

date | awk '{printf("%s%3d\n", $2, $3)}

---------- Post updated at 02:32 PM ---------- Previous update was at 02:23 PM ----------

Don/Franklin

can you explain this? character by character?? Please???

thanks

awk '{printf("%s%3d\n", $2, $3)}'
 awk '{ printf("%s %*s\n", $2, 2, $3) }'

sed 's/....\(......\).*/\1/'




awk '{printf("%s%3d\n", $2, $3)}'
  1. awk : utility to run.
  2. '{printf("%s%3d\n", $2, $3)}' : script to be interpreted by awk .
  3. '{ commands }' : run the specified commands for each line read from standard input.
  4. printf(format, arg1, arg2) : print the specified arguments to standard output according to the formatting specifiers given in format.
  5. "%s" : format the next argument ($2 in this case) as a string.
  6. "%3d" : format the next argument ($3 in this case) as a 3 character string of decimal digits with leading space fill.
  7. "\n" : print a literal <newline> character.
  8. , : separate arguments in the parameter list of the function.
  9. $2 : the 2nd field in the current input line. (By default, awk splits fields by sequences of one or more space and/or tab characters.)
  10. $3 : the 3rd field in the current input line.
awk '{ printf("%s %*s\n", $2, 2, $3) }'
  1. awk : utility to run.
  2. '{printf("%s %*s\n", $2, $3)}' : script to be interpreted by awk .
  3. '{ commands }' : run the specified commands for each line read from standard input.
  4. printf(format, arg1, arg2) : print the specified arguments to standard output according to the formatting specifiers given in format.
  5. "%s" : format the next argument ($2 in this case) as a string.
  6. " " : print a literal space character.
  7. "%*s" : format a string with width specified by the next argument (2 in this case) as right justified with leading space fill from the next argument ($3 in this case).
  8. "\n" : print a literal <newline> character.
  9. , : separate arguments in the parameter list of the function.
  10. $2 : the 2nd field in the current input line.
  11. 2 : the integer two.
  12. $3 : the 3rd field in the current input line.
sed 's/....\(......\).*/\1/'
  1. sed : the utility to run.
  2. 's/....\(......\).*/\1/' : the sed command to be executed.
  3. s/BRE/replacement/ : for each line read from standard input, substitute the string specified by repacement for the string in the input line that matches the basic regular expression specified by BRE.
  4. .... : match any four contiguous characters.
  5. \(......\) : match any six contiguous characters and remember them for use as a back reference.
  6. .* match the longest string of zero or more available characters. These three parts of the BRE when concatenated together match the entire input line and remember the 5th through the 10th characters on the line as a back reference.
  7. \1 : replace the entire string matched by the BRE with the 1st substring remembered as a back reference.
  8. and print the modified input line.
1 Like